微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

通过一个查询中的数据像样条一样按日期分组

如何解决通过一个查询中的数据像样条一样按日期分组

我具有下一个结构中存储在ClickHouse表中的相似主机的时间序列数据:

 event_type | event_day        
------------|---------------------
 type_1     | 2017-11-09 20:11:28
 type_1     | 2017-11-09 20:11:25
 type_2     | 2017-11-09 20:11:23
 type_2     | 2017-11-09 20:11:21

表中的每一行表示datetime上event_type的值为1。为了快速评估情况,我需要指出总和(总计)+最后七个值(脉冲),如下所示:

 event_type | day        | total | pulse                       
------------|------------|-------|-----------------------------
 type_1     | 2017-11-09 | 876   | 12,9,23,67,5,34,10          
 type_2     | 2017-11-09 | 11865 | 267,120,234,425,102,230,150 

我尝试通过以下方式通过一个请求获取它,但失败了-脉冲包含相同的值:

with
    arrayMap(x -> today() - 7 + x,range(7)) as week_range,arrayMap(x -> count(event_type),week_range) as pulse
select
    event_type,toDate(event_date) as day,count() as total,pulse
from database.table
group by day,event_type
 event_type | day        | total | pulse                       
------------|------------|-------|-------------------------------------------
 type_1     | 2017-11-09 | 876   | 876,876,876          
 type_2     | 2017-11-09 | 11865 | 11865,11865,11865 

请指出我的错误在哪里以及如何获得期望?

解决方法

我会考虑在服务器端计算 pulse ,CH只提供所需的数据。


可以使用neighbor-window function

SELECT
    number,[neighbor(number,-7),neighbor(number,-6),-5),-4),-3),-2),-1)] AS pulse
FROM
(
    SELECT number
    FROM numbers(10,15)
    ORDER BY number ASC
)

┌─number─┬─pulse──────────────────┐
│     10 │ [0,0]        │
│     11 │ [0,10]       │
│     12 │ [0,10,11]      │
│     13 │ [0,11,12]     │
│     14 │ [0,12,13]    │
│     15 │ [0,13,14]   │
│     16 │ [0,14,15]  │
│     17 │ [10,15,16] │
│     18 │ [11,16,17] │
│     19 │ [12,17,18] │
│     20 │ [13,18,19] │
│     21 │ [14,19,20] │
│     22 │ [15,20,21] │
│     23 │ [16,21,22] │
│     24 │ [17,22,23] │
└────────┴────────────────────────┘
,
select event_type,groupArray(1)(day)[1],arraySum(pulse) total7,groupArray(7)(cnt) pulse
from (
    select
      event_type,toDate(event_date) as day,count() as cnt
    from database.table
    where day >= today()-30
    group by event_type,day
    order by event_type,day desc 
)
group by event_type 
order by event_type

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。