sql - How to refine last but one? -
I have the following table I need to get the last but one event associate for every event
event_id event_date event_associate 1 2/14/2014 bin 1 2/15/2014 Ben 1 2/16/2014 Steve 1 2 / 17/2014 Steve // This is the last one, but for the event a 1 1 2 / 18/2014 Paul 2 2/19/2014 Paul 2 2/20/2014 paul // This colleague is the last but for the event 2 2 2/21/2014 Bin 3 2/22/2014 Paul 3 2/23/2014 Paul 3 2/24/2014 Bin 3 2/25/2014 Steve // This is the last but the event is one for 3 3/2/26/2014 Ben
I have to find out who was the last who had a event_associate for each event
event_id event_associate 1 steve 2 paul 3 steve
I know that to do this, I want to do the maximum Event_date and event_associate
I tried to exclude
select event_id, where event_associate is not present (select * from mytable WHERE Event_date = MAX (event_date)) Qualification at row_NUMBER () (order event_date according to Event_id DESC By) = 1
But I do not know how to EXISTS
in this case.
You are very close, you just need a second line based on ROW_NUMBER:
< Choose pre> *, More than Row_number (), the event_date desc by split by event_date desc) on the qualification row_number () by tab (event_date desc divided by event_id command) = 2 - or simply-qualified RN = 2
Comments
Post a Comment