ClickHouse 聚合 - 按日/月/年(时间戳)分组?

sve*_*lav 2 mysql group-by aggregate aggregate-functions clickhouse

ClickHouse 有没有办法用时间戳值执行 GROUP BY DAY/MONTH/YEAR() ?在将 MySQL 查询重写为 ClickHouse 时,很难弄清楚这一点。我的 MySQL 查询看起来像这样......

SELECT COUNT(this), COUNT(that) FROM table WHERE something = x AND stamp BETWEEN startdate AND enddate

SELECT COUNT(this), COUNT(that) FROM table WHERE something = x AND stamp BETWEEN startdate AND enddate GROUP BY DAY(stamp)

SELECT COUNT(this), COUNT(that) FROM table WHERE something = x AND stamp BETWEEN startdate AND enddate GROUP BY MONTH(stamp)

SELECT COUNT(this), COUNT(that) FROM table WHERE something = x AND stamp BETWEEN startdate AND enddate GROUP BY YEAR(stamp)
Run Code Online (Sandbox Code Playgroud)

在 MySQL 中非常简单且缓慢,但我不知道如何在 ClickHouse 中进行聚合。

谢谢!

vla*_*mir 6

要获取日期的一部分,请使用函数toYeartoMonthtoDayOfMonth通过以下方式

\n
SELECT\n    toMonth(time) AS month,\n    count()\nFROM \n(\n    SELECT\n        number,\n        addDays(now(), number) AS time\n    FROM numbers(8)\n)\nGROUP BY month\n\n/*\n\xe2\x94\x8c\xe2\x94\x80month\xe2\x94\x80\xe2\x94\xac\xe2\x94\x80count()\xe2\x94\x80\xe2\x94\x90\n\xe2\x94\x82     1 \xe2\x94\x82       7 \xe2\x94\x82\n\xe2\x94\x82     2 \xe2\x94\x82       1 \xe2\x94\x82\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xb4\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x98\n*/\n
Run Code Online (Sandbox Code Playgroud)\n
\n

要获取多个分组集,请使用WITH ROLLUP -修饰符:

\n
SELECT\n    toYear(time) AS year,\n    toMonth(time) AS month,\n    toDayOfMonth(time) AS day,\n    count()\nFROM \n(\n    SELECT\n        number,\n        addDays(now(), number) AS time\n    FROM numbers(8)\n)\nGROUP BY\n    year,\n    month,\n    day\n    WITH ROLLUP\n\n/*\n\xe2\x94\x8c\xe2\x94\x80year\xe2\x94\x80\xe2\x94\xac\xe2\x94\x80month\xe2\x94\x80\xe2\x94\xac\xe2\x94\x80day\xe2\x94\x80\xe2\x94\xac\xe2\x94\x80count()\xe2\x94\x80\xe2\x94\x90\n\xe2\x94\x82 2021 \xe2\x94\x82     2 \xe2\x94\x82   1 \xe2\x94\x82       1 \xe2\x94\x82 // day\n\xe2\x94\x82 2021 \xe2\x94\x82     1 \xe2\x94\x82  29 \xe2\x94\x82       1 \xe2\x94\x82 // day\n\xe2\x94\x82 2021 \xe2\x94\x82     1 \xe2\x94\x82  31 \xe2\x94\x82       1 \xe2\x94\x82 // day\n\xe2\x94\x82 2021 \xe2\x94\x82     1 \xe2\x94\x82  26 \xe2\x94\x82       1 \xe2\x94\x82 // day\n\xe2\x94\x82 2021 \xe2\x94\x82     1 \xe2\x94\x82  25 \xe2\x94\x82       1 \xe2\x94\x82 // day\n\xe2\x94\x82 2021 \xe2\x94\x82     1 \xe2\x94\x82  28 \xe2\x94\x82       1 \xe2\x94\x82 // day\n\xe2\x94\x82 2021 \xe2\x94\x82     1 \xe2\x94\x82  30 \xe2\x94\x82       1 \xe2\x94\x82 // day\n\xe2\x94\x82 2021 \xe2\x94\x82     1 \xe2\x94\x82  27 \xe2\x94\x82       1 \xe2\x94\x82 // day\n\xe2\x94\x82 2021 \xe2\x94\x82     1 \xe2\x94\x82   0 \xe2\x94\x82       7 \xe2\x94\x82 // month\n\xe2\x94\x82 2021 \xe2\x94\x82     2 \xe2\x94\x82   0 \xe2\x94\x82       1 \xe2\x94\x82 // month\n\xe2\x94\x82 2021 \xe2\x94\x82     0 \xe2\x94\x82   0 \xe2\x94\x82       8 \xe2\x94\x82 // year\n\xe2\x94\x82    0 \xe2\x94\x82     0 \xe2\x94\x82   0 \xe2\x94\x82       8 \xe2\x94\x82\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xb4\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xb4\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xb4\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x98\n*/\n
Run Code Online (Sandbox Code Playgroud)\n