RICH-ATONE

ClickHouse实现类似Hive的row_number() over (partition by)的方法

利用rowNumberInAllBlocks函数:

hive写法:

select orderid
  from (select orderid,
               row_number() over(partition by orderid order by datachange_lasttime desc) as row_num
          from table
         where d = '${CurrentDate}'
         ) a
 where row_num = 1;

ClickHouse写法,使用rowNumberInAllBlocks函数。(注:使用其他几种CK的实现方式,当按照指定字段分组,查询所有表字段的最新一条时,是实现不了的

select orderid, status
  from (select orderid, status, rowNumberInAllBlocks() as rank
          from (select orderid, status, datachange_lasttime
                  from table
                 order by orderid, datachange_lasttime desc
                 ) a
       ) b LIMIT 1 BY orderid

  简写方式,方法同上(LIMIT 1 BY 方式):

select orderid, 
       status, 
       datachange_lasttime
  from table
 order by orderid, datachange_lasttime desc 
 LIMIT 1 BY orderid

  

参考:https://blog.csdn.net/Jarry_cm/article/details/106115044

posted on 2022-10-24 11:24  RICH-ATONE  阅读(914)  评论(0编辑  收藏  举报

导航