Take the 2-minute tour ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

I have a query which selects a max value over all the values of in a datetime column (dtimesent).

select dtimesent, max(ResponseTime) AS maxtime
FROM allrequests 
where Request like '%Requestsa%'
group by dtimesent

I need to get the value of the request colmn for each max response time in each group by. Is there any easy way to do this? There can be multiple rows per dtimesent value so I can't just include the request column in the group by,

Many thanks,

share|improve this question
    
Easy way to do what? You've made a statement, not asked a question - edit it for clarity please ;) –  FreshPhilOfSO May 17 '12 at 8:48
    
@Phil done thanks. –  dublintech May 17 '12 at 8:52
add comment

1 Answer

RANK will deal with multiple max values (instead of ROW_NUMBER)

SELECT
   dtimesent, Request, ResponseTime
FROM
    (
    select dtimesent, Request, ResponseTime ,
       RANK() OVER (ORDER BY ResponseTime DESC) AS rn
    FROM allrequests 
    where Request like '%Requestsa%'
    ) T
WHERE
   rn = 1

Also see How to get the MAX row please

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.