如何将时间范围行拆分为序列第二级行

问题描述 投票:0回答:1

例如,在我的 clickhouse 实例中,有 4 行

uuid  |  time     | counter
333   | 13:13:13  | 13
333   | 13:13:15  | 133

3333  | 23:23:23  | 23
3333  | 23:23:25  | 233

如何将2个时间范围行拆分为序列第二级行,例如

uuid  |  time     | counter
333   | 13:13:13  | (133-13)/3
333   | 13:13:14  | (133-13)/3
333   | 13:13:15  | (133-13)/3

3333  | 23:23:23  | (233-23)/3
3333  | 23:23:23  | (233-23)/3
3333  | 23:23:25  | (233-23)/3

扫描了clickhouse的时间序列函数,但没有找到解决方案。

sql clickhouse
1个回答
0
投票

试试这个方法:

WITH data AS
    (
        SELECT *
        FROM VALUES('uuid UInt32, time DateTime, counter UInt32', 
          (333,  '2000-01-01 13:13:13', 13), 
          (333,  '2000-01-01 13:13:15', 133), 
          (3333, '2000-01-01 23:23:23', 23), 
          (3333, '2000-01-01 23:23:25', 233))
    )
SELECT
    uuid,
    from_time + offset AS time,
    value AS counter
FROM
(
    SELECT
        uuid,
        min(time) AS from_time,
        (max(time) - from_time) + 1 AS lag_sec,
        (max(counter) - min(counter)) / lag_sec AS value
    FROM data
    GROUP BY
        uuid,
        toStartOfMinute(time)
)
ARRAY JOIN range(lag_sec) AS offset

/*
┌─uuid─┬────────────────time─┬─counter─┐
│  333 │ 2000-01-01 13:13:13 │      40 │
│  333 │ 2000-01-01 13:13:14 │      40 │
│  333 │ 2000-01-01 13:13:15 │      40 │
│ 3333 │ 2000-01-01 23:23:23 │      70 │
│ 3333 │ 2000-01-01 23:23:24 │      70 │
│ 3333 │ 2000-01-01 23:23:25 │      70 │
└──────┴─────────────────────┴─────────┘
*/
© www.soinside.com 2019 - 2024. All rights reserved.