Clickhouse 实现 row number功能

参考博客:https://blog.csdn.net/qq_41018861/article/details/118723297

1、建表

CREATE TABLE default.row_number
(
    `user_id` String,
    `user_phone` String,
    `create_time` DateTime,
    `update_time` DateTime
)
ENGINE = MergeTree
ORDER BY user_id
SETTINGS index_granularity = 8192;

2、插入语句

insert into default.row_number values
('1','13234567891','2021-07-14 11:54:19','2021-07-14 11:54:19'),
('1','13234567890','2021-07-14 11:40:09','2021-07-14 11:40:09'),
('1','13234567892','2021-07-14 11:54:37','2021-07-14 11:54:37'),

('2','13234567894','2021-07-14 11:57:22','2021-07-14 11:57:22'),
('2','13234567893','2021-07-14 11:55:05','2021-07-14 11:55:05'),
('3','13234567896','2021-07-14 11:57:45','2021-07-14 11:57:45'),
('3','13234567895','2021-07-14 11:57:30','2021-07-14 11:57:30')

3、row number 取最新一条查询

select user_id, 
      groupArray(1)(user_phone) AS user_phone,
      groupArray(1)(create_time) AS create_time,
      groupArray(1)(update_time) AS update_time
 from (select user_id, 
              user_phone,
              create_time,
               update_time
         from default.row_number
        ORDER BY update_time desc
       ) a
group by user_id
order by user_id

4、结果

 

posted @ 2021-12-29 09:09  小白啊小白,Fighting  阅读(1587)  评论(0编辑  收藏  举报