This question already has answers here:
row with minimum value of a column
(7 answers)
Closed 6 years ago.
I have a data with three columns as below:
lic_plt_id qty prod_qty
--------------------------------
123456 3 44556
345567 50 44556
098765 13 44556
result required is where qty is minimum with below detail.
lic_plt_id qty prod_qty
----------------------------
123456 3 44556
SELECT TOP 1 *
FROM Tablename
ORDER BY qty
this will order the table by qty and then selects the first one which is the lowest number.
Related
This question already has answers here:
How to use RANK() in SQL Server
(8 answers)
Closed 3 years ago.
How can I create a sequential value based on two rows within a table, for example, let's say I have a table containing an employee's ID and work state. I would expect the following values:
ID State Expected Value
-----------------------------
1 NY 1
1 PA 2
1 NY 1
2 NC 1
2 FL 2
2 MN 3
You can use dense_rank():
select t.*,
dense_rank() over (partition by id order by state) as expected
from t;
This question already has answers here:
Calculate a Running Total in SQL Server
(15 answers)
Closed 4 years ago.
Every record amount sum into Total column
id Amount total
--------------------------------------------
1 100 100
1 50 150
1 60 210
1 10 220
2 70 290
2 10 300
Any suggestion would be very much appreciated.
You want window function :
select t.*,
sum(amount) over (order by id rows between unbounded preceding and current row) as total
from table t;
This question already has answers here:
How to get second largest or third largest entry from a table [duplicate]
(12 answers)
Closed 4 years ago.
I have the following data:
id date mia
1 1/1/2017 3
1 1/2/2017 1
1 1/3/2017 2
2 1/4/2017 1
2 1/5/2017 4
2 1/6/2017 6
.
.
.
.
and so on.
If I give input as id=1 I should fetch record of 2017-02-01 and if input id=2 then record of 2017-05-01 i.e record of previous month of the highest date.
You could use:
SELECT *
FROM (SELECT *, ROW_NUMBER() OVER(PARTITION BY id ORDER BY mia DESC) AS rn
FROM table) sub
WHERE rn = 2;
you may not define a column named as date, instead i use date_.
For former versions you may refer to #lad2025 's answer, if you're on oracle12c, you may query with the following :
select min(date_) min_date
from
(
select date_
from mytable
where id = &i_id
group by id, date_
order by date_
fetch first 2 rows only
);
This question already has answers here:
Efficiently convert rows to columns in sql server
(5 answers)
Closed 6 years ago.
if I have a table like this:
MatchCode TransferId
---------- -----------
17edce4d 7
17edce4d 17
20332cf0 22
20332cf0 30
is it possible make the result return this?
MatchCode TransferId1 TransferId2
---------- ----------- -----------
17edce4d 7 17
20332cf0 22 30
If each MatchCode group will always have only two records, then you can use the MIN and MAX along with a GROUP BY:
SELECT MatchCode, MIN(TransferId) AS TransferId1, MAX(TransferId) AS TransferId2
FROM yourTable
GROUP BY MatchCode
This question already has answers here:
Select newest record group by username in SQL Server 2008
(4 answers)
Closed 7 years ago.
When we use group by some column information is lost ,I am looking for a query such that all columns of the table is returned while getting the latest data.
Eg for following source data the expected result is all items quantity for latest date.
Data:
Item Quantity Date
Apple 7 7/17/2015
Ball 15 7/17/2015
Cat 10 7/17/2015
Dog 8 7/19/2015
Ball 1 7/19/2015
Desired Result:
Item Quantity Date
Apple 7 7/17/2015
Ball 1 7/19/2015
Cat 10 7/17/2015
Dog 8 7/19/2015
Here if we use group by we loose the Quantity as it will be neither be in group by nor in aggregation.
This table does not have a id key so subquery could not be used.
You would use row_number():
select item, quantity, date
from (select t.*,
row_number() over (partition by item order by date desc) as seqnum
from t
) t
where seqnum = 1;