teradata - How to get the last two rows -


In the table, I need to get past two event associates for each event

  Event_id Event_date event_associate 1 2/14/2014 Bin 1 2/15/2014 Bin 1 2/16/2014 Steve 1 2/17/2014 Steve // ​​This partner is final but for the event 1 1 2/18/2014 Paul / This is the last for a cooperative event 1 2 2/19/2014 Paul 2 2/20/2014 paul // This is the last but the event for a 2 2/21/2014 Ban / This associate is for the past Event 2 3 2/22/2014 Paul 3 2/23/2014 Paul 3 2/24/2014 Bin 3 2/25/2014 ET // This is the last but this is a 3 3/2/26/2014 Ben / / This is the last for partner event 3  

I have to find out that for every event For the last but who had a event_associate should be the result

  event_id event_associate rn 1 Steve 2 1 Paul 1 2 Paul 2 2 Ben 1 3 Steve 2 3 Ben 1  

I tried

  SELECT t *, ROW_NUMBER (above) by event_date (via event_date DESC event transit order) RN mytable T QUALIFY rn & lt; 3  

"for each event" -> partition Event_id by

"last but one" -> event_date by the ORDER DESC


Comments