Compare the same field on the same table for the same stock code [closed] - sql

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I have the following Table:
StockCode Bin
123 L02A3
123 L08B2
123 L08C2
456 L04B2
456 L02C1
789 L19A2
When looking at a Bin for a stock code, any bin that contains an "A" is on the floor level of the warehouse. If the bin contains a "B", it is on the middle rack of the warehouse, and if the bin contains a "C", it is on the top rack of the warehouse.
I would like a sql query that compares all the bins for each stock code and shows which stock codes do not have a current bin on the "A" level.
From the example table above, stock code "456" would show because it is the only stock code that does not have inventory in an "A" bin.
This will be very helpful because it will flag us to which inventory to move down to the floor level before the pick tickets are printed.
Thank you in advance for any help!

Think group by and having:
select stockcode
from t
group by stockcode
having sum(case when bin like '%A_' then 1 else 0 end) = 0;
The having clause counts the number of rows for each stock code where the bin has an A (this assumes that the penultimate letter is an A, as in the examples in the question). If the number is 0, then the stock code is returned.

Related

How to fetch data using where clause in SQL [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 months ago.
Improve this question
There is no error, but the results show the values excluding the row that meets both conditions. The result shows 3, in fact, it should be 4 excluding data that has 22 for age and UK for the country. Any idea why the result is 3, not 4?
SELECT COUNT(*) FROM CUSTOMERS WHERE CUSTOMERS.AGE = '22' OR CUSTOMERS.COUNTRY = 'UK';
You have two customers at the age of 22 and 2 residing in the UK.
This is actually a correct result here - how on earth do you come to the conclusion that you have four people meeting that criteria? If more than one point matches for a database entry, it will only count as one for the result, since it's still the same database entry.

How to get column values as a table column in sql [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
This is my table defined in the database:
pi_value | Status
------------+-------------------
500.000 | Bank Submitted
500.0000 | Bank Submitted
1000.0000 | Maturity Received
4000.0000 | Bank Submitted
50.0000 | Maturity Received
I want the output to look like this:
Maturity Received | Bank Submitted |
------------------+----------------+
1050.0000 | 5000.0000 |
------------------+----------------+
use conditional aggregation
select sum(case when status='Bank Submitted' then pi_value else 0 end),
sum(case when status='Maturity Received' then pi_value else 0 end) from table
As you say in a comment that the status is not before-known, just select the mere data and have your app or website do the layout:
select status, sum(pi_value) as total
from mytable
group by status
order by status;
Whatever programming language you are using, it will be easy to simply loop through the result set and fill some grid with it.
Alternative:
If you want to get the pivoted data right away, you'll have to use dynamic SQL instead. This means you select the data needed to build the query first, then you build the query from it and run it. Here is how to get the statuses:
select distinct status from mytable order by status;
Then loop through these and construct a query like shown in Zaynul Abadin Tuhin's answer. Then run that query.

How can I count repeat rows in SQL Server 2012? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I want to assign one unique number for each repeated row in select statement.
table
Name | Position
Mark Wol 1
Mark wol 1
MArtha 1
Martha 1
and I want this
1 Mark Wl 1
2 Martha 1
You can use as few or as many rows as you want. If you want to use all of the rows, list them all.
SELECT Row1, Row2, Row3,
ROW_NUMBER() OVER (
PARTITION BY Row1, Row2, Row3,
ORDER BY Row1
)
FROM Table
The best solution is to already have a unique ID for each record and then you are choosing to show that (which will come from the first found record)...
SELECT name,COUNT(*),id
FROM test_table
GROUP BY name
HAVING COUNT(*) > 1

SQL select statement to Exclude a column from being sorted [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I am looking for a way to exclude a single column from being sorted using SQL select statement.
Lets say a table has 10 columns and when an ORDER BY is done on a specific column using select statement, all the 10 columns are sorted. Is it possible to exclude any specific column (say column 6) from being sorted? If so how.
**
Before
ID name
-----------
1 papaya
2 apple
3 strawberry
4 banana
Required
ID name
-----------
1 apple
2 banana
3 papaya
4 strawberry
Appreciate your help
This will generate the ids and they will be always shown in order but that won't be the actual id, you might need that as well
SELECT #a:=#a+1 id,name from tableName order by name

SQL: Generating a field based on a field [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a table (for the sake of the exercise let's call it Persons)
I am trying to create a query that will generate the CrDRN number.
The CrDRN column should contain the DRN number of the last record that has a VoucherType = 80
In this instance the CrDRN column would have the values 1 1 1 5 5
Any help would be greatly appreciated.
The CrDRN column should contain the DRN number of the last record that
has a VoucherType = 80
Another way of saying that would be to get the max value of DRN where DRN is less than or equal to the current DRN and where VoucherType is 80.
You can formulate just that in a subquery that returns CrDRN.
select T1.DRN,
T1.VoucherType,
(
select max(T2.DRN)
from T as T2
where T2.DRN <= T1.DRN and
T2.VoucherType = 80
) as CrDRN
from T as T1