假设我有一个给定的时间范围。为了进行解释,让我们考虑一些简单的事情,例如整个2018年。我想从ClickHouse查询数据作为每个季度的总和,因此结果应为4行。
问题是我只有两个季度的数据,因此使用时GROUP BY quarter,仅返回两行。
SELECT
toStartOfQuarter(created_at) AS time,
sum(metric) metric
FROM mytable
WHERE
created_at >= toDate(1514761200) AND created_at >= toDateTime(1514761200)
AND
created_at <= toDate(1546210800) AND created_at <= toDateTime(1546210800)
GROUP BY time
ORDER BY time
Run Code Online (Sandbox Code Playgroud)
1514761200– 2018-01-01
1546210800–2018-12-31
返回:
time metric
2018-01-01 345
2018-04-01 123
Run Code Online (Sandbox Code Playgroud)
我需要:
time metric
2018-01-01 345
2018-04-01 123
2018-07-01 0
2018-10-01 0
Run Code Online (Sandbox Code Playgroud)
这是简化的示例,但是在实际使用情况下,聚合将是例如。5分钟而不是四分之一,GROUP BY将至少具有一个以上属性,GROUP BY attribute1, time因此期望的结果是
time metric attribute1
2018-01-01 345 1
2018-01-01 345 2
2018-04-01 123 1
2018-04-01 …Run Code Online (Sandbox Code Playgroud)