clickhuose toStartOfInterval聚合的几种方式

1. 秒、分钟、小时、天

以上这种sql可以用于上面几种类型的聚合。请注意时间

SELECT 
    toStartOfInterval(timestamp, INTERVAL 1 minute) as start_of_minute,
    count(*) as count FROM table_name
WHERE 
	timestamp BETWEEN '2023-10-30 11:11:11' and '2023-11-30 11:11:11'
group by start_of_minute

 2. 周、月

SELECT 
    toStartOfInterval(timestamp, INTERVAL 1 minute) as start_of_week,
    count(*) as count FROM table_name
WHERE 
	timestamp BETWEEN '2023-10-30' and '2023-11-30'
group by start_of_week

周和月的时间必须为yyyy-MM-dd ,如果使用yyyy-MM-dd xxxx 的格式,clickhouse无法识别

官网例子:
https://clickhouse.com/docs/en/sql-reference/functions/date-time-functions#tostartofintervaldate_or_date_with_time-interval-x-unit--time_zone

posted @ 2023-11-30 16:01  XSWClevo  阅读(1044)  评论(0)    收藏  举报