How to get the latest records in hive using rank function

∥☆過路亽.° 提交于 2019-12-12 18:31:04

问题


I have below table in hive with column id, name and time stamp:

On the basis of time stamp below should be the output as latest record:


回答1:


You don't need rank for this. Your output is described by:

select t.*
from t
order by t.transaction_time desc
limit 3;

EDIT:

Oh, you want rank() or dense_rank():

select t.*
from (select t.*,
             dense_rank() over (order by t.transaction_time desc) as seqnum
      from t
     ) t
where seqnum = 1;



回答2:


You can use either rank or row_number for this:

select *
from (
    select t.*,
        row_number() over (
            partition by name 
            order by transaction_time desc
            ) as seq
    from your_table t
    ) t 
where seq = 1;


来源:https://stackoverflow.com/questions/46306473/how-to-get-the-latest-records-in-hive-using-rank-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!