Query to get one entry from multiple rows
Query:
select machinename, StatusCode, max(size) as size, statusID
from machine where MachineID In( '33','22') and StatusCode = 166
group by machinename, StatusCode, statusID
order by max(size) DESC
Result:
machinename StatusCode size statusID
----------- ---------- ---- --------
test1 166 50 1
test1 166 25 2
test2 166 75 3
test2 166 48 4
Requirement:
I need to display only one entry for each machine. I have to do this by taking the max size value between the two entries as shown above. like for test1 i have two sizes 50 and 25 I 开发者_开发技巧have to show the row which has 50 and ignore row which has 25.
Thanks
Desired Result:
machinename StatusCode size statusID
----------- ---------- ---- --------
test1 166 50 1
test2 166 75 3
SELECT machinename, StatusCode, size, statusID
FROM (
SELECT
machinename,
StatusCode,
size,
statusID,
ROW_NUMBER() OVER (PARTITION BY MachineID ORDER BY size DESC) AS rn
FROM machine
WHERE MachineID IN ('33','22')
AND StatusCode = 166
) T1
WHERE rn = 1
ORDER BY size DESC
精彩评论