3

Query:

select machinename, StatusCode, size
from machine where MachineID In( '33','22') and StatusCode = 166 
ORDER BY size DESC 

Result:

machinename  StatusCode  size
-----------  ----------  ----
test1        166         50
test1        166         25
test2        166         75
test2        166         48

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
-----------  ----------  ----
test1        166         50
test2        166         75
3
  • What exactly are you trying to accomplish? I don't understand what you mean by "where the size is greater than the other." Commented Sep 28, 2011 at 23:36
  • I don't understand what you mean by where the size is greater than the other. Are you just looking for the max size for that machine name? Commented Sep 28, 2011 at 23:37
  • eg for machine test1, i have two sizes 50 and 25 so i want to display only the row with size 50 and not 25. let me know if i need to explain mare Commented Sep 28, 2011 at 23:41

4 Answers 4

4

This will work, but you won't be able to order by starttime

select machinename, StatusCode, max(size) as size
from machine where MachineID In( '33','22') and StatusCode = 166 
group by machinename, StatusCode
order by max(size) DESC
Sign up to request clarification or add additional context in comments.

3 Comments

+1 This is best for updated query. Except you have missed a DESC.
Actually it would be possible to ORDER BY MAX(StartTime).
Yes, but that wouldn't tie to the same record that had the max(size), so all it would really tell you is when the last time this machine had a test
3

If you wanted to order by StartTime you would have to use ROW_NUMBER so that you could select the starttime field:

SELECT machinename, StatusCode, size
FROM (
    SELECT
        machinename,
        StatusCode,
        StartTime
        size,
        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 StartTime DESC

But if you want to order by size, it's easier:

SELECT machinename, StatusCode, MAX(size) AS size
FROM machine
WHERE MachineID IN ('33','22') AND StatusCode = 166 
GROUP BY MachineID
ORDER BY MAX(size) DESC

1 Comment

sorry but the server went down. will test and check correct answer
2

Use a windowing function to generate ids to partition on your key and then order by largest to smallest. This will generate 1-N for each partition where 1 will correspond to the largest value from the ORDER BY. We then filter for the largest one.

; WITH MACHINES AS
(
select machinename, StatusCode, size
, ROW_NUMBER() OVER (PARTITION BY machinename ORDER BY size DESC) AS RN
from machine where MachineID In( '33','22') and StatusCode = 166 

)
SELECT
select machinename, StatusCode, size
FROM MACHINES M
WHERE M.rn = 1;

Comments

2
select machinename, StatusCode, size
from machine a
where MachineID In( '33','22') and StatusCode = 166 
and size = (select max(size) 
from machine b
where MachineID In( '33','22') and StatusCode = 166 
and a.machinename = b.machinename)
ORDER BY StartTime DESC

HTH.

1 Comment

sorry but the server went down. will test and check correct answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.