需求
HIVE中怎么取最后一条记录?
解决方案
使用函数:row_number() over([partition_by_clause] order_by_clause)
原始数据
图片.png按时间排序
select id,
start_time,
end_time,
row_number() over(order by start_time desc) as rank
from call_record
输出:
图片.png
以ID分组按时间排序
select id,
start_time,
end_time,
row_number() over(partition by id order by start_time desc) as rank
from call_record
输出:
图片.png
以ID分组按时间排序取最后一条记录
select*
from(
select id,
start_time,
end_time,
row_number() over(partition by id order by start_time desc) as rank
from call_record
)a
where a.rank=1
输出
图片.png
如果要取第一条记录 desc改为asc
网友评论