Teradata 最后两行怎么走
桌子是空的 我需要为每个活动找到最后两名活动助理Teradata 最后两行怎么走,teradata,Teradata,桌子是空的 我需要为每个活动找到最后两名活动助理 event_id event_date event_associate 1 2/14/2014 ben 1 2/15/2014 ben 1 2/16/2014 steve 1 2/17/2014 steve // this associate is the last but one for event 1 1 2/18/
event_id event_date event_associate
1 2/14/2014 ben
1 2/15/2014 ben
1 2/16/2014 steve
1 2/17/2014 steve // this associate is the last but one for event 1
1 2/18/2014 paul // this associate is the last for event 1
2 2/19/2014 paul
2 2/20/2014 paul // this associate is the last but one for event 2
2 2/21/2014 ben // this associate is the last for event 2
3 2/22/2014 paul
3 2/23/2014 paul
3 2/24/2014 ben
3 2/25/2014 steve // this associate is the last but one for event 3
3 2/26/2014 ben // this associate is the last for event 3
我需要找出谁是最后一个,但每个事件只有一个事件\u助理。结果应该是
event_id event_associate rn
1 steve 2
1 paul 1
2 paul 2
2 ben 1
3 steve 2
3 ben 1
我试过了
SELECT t.* , ROW_NUMBER() OVER (PARTITION BY event_associate ORDER BY event_date DESC) rn
FROM mytable t
QUALIFY rn < 3
选择t.*,行号()在(按事件划分\u按事件关联顺序\u日期描述)rn上
来自MyT表
合格rn<3
您必须计算数据库中的行数,然后使用此查询
sql=sql+" LIMIT "+NumberOfRowsToShowInTables+" OFFSET "+(countrow- NumberOfRowsToShowInTables);
其中countrow
是数据库中的行数
numberofrowstoshowTables
如您所述等于2
sql是您对每个事件的正常查询,不受限制
“最后一个但只有一个”->按事件的顺序\u date DESC1st:这应该是Teradata SQL,但TD中没有限制/偏移。第二:即使这是有效的SQL,它也不会为每个事件返回一行