ClickHouse:一种有效的方式,可以按以下时间范围来聚合数据

我需要在不同的时隙上汇总时间序列数据(具有平均功能),例如:

  • 今天
  • 最近X天
  • 上周末
  • 这周
  • 过去X周
  • 本月
  • 列表项

    等...

问题1:是否可以在GROUP BY语句中完成,或者至少使用一个查询?

第二季度:我是否需要任何物化视图?

该表由Month分区,并由UserID分片

所有查询都在UserID之内(单碎片)

wannabe321 回答:ClickHouse:一种有效的方式,可以按以下时间范围来聚合数据

通过ROLLUP分组

create table xrollup(metric Int64,b date,v Int64 ) engine=MergeTree partition by tuple() order by tuple();
insert into xrollup values (1,'2018-01-01',1),(1,'2018-01-02','2018-02-01','2017-03-01',1);
insert into xrollup values (2,(2,'2018-02-02',1);

SELECT metric,toYear(b) y,toYYYYMM(b) m,SUM(v) AS val
FROM   xrollup
GROUP BY metric,y,m  with ROLLUP
ORDER BY metric,m 

┌─metric─┬────y─┬──────m─┬─val─┐
│      0 │    0 │      0 │   6 │  overall
│      1 │    0 │      0 │   4 │  overall by metric1
│      1 │ 2017 │      0 │   1 │  overall by metric1 for 2017
│      1 │ 2017 │ 201703 │   1 │  overall by metric1 for march 2017
│      1 │ 2018 │      0 │   3 │
│      1 │ 2018 │ 201801 │   2 │
│      1 │ 2018 │ 201802 │   1 │
│      2 │    0 │      0 │   2 │
│      2 │ 2018 │      0 │   2 │
│      2 │ 2018 │ 201801 │   1 │
│      2 │ 2018 │ 201802 │   1 │
└────────┴──────┴────────┴─────┘
本文链接:https://www.f2er.com/3027120.html

大家都在问