当前位置:网站首页>ClickHouse字段分组聚合、按照任意时间段粒度查询SQL
ClickHouse字段分组聚合、按照任意时间段粒度查询SQL
2022-07-06 17:32:00 【坚持是一种态度】
ClickHouse字段分组聚合、按照任意时间段粒度查询SQL
需求
- 基础数据入库,需要根据字段做一些聚合
- 现有路口过车数据,有各种车型,需要统计总流量和各个车型流量,要一起返回
- 现有过车数据,需要按照时间不同粒度,统计车流量,实现不同时间粒度求和,5分钟、10分钟、半小时、一个月、一年都行
解决
使用if和sum
- 主要是使用
if
,符合条件为1,不符合为0,再使用sum
求和
select
count(1) as "总流量",
SUM(if(vehicle_type = 'PEDESTRIAN', 1, 0)) as "行人流量",
SUM(if(vehicle_type = 'NON_MOTORIZED', 1, 0)) as "非机动车流量",
SUM(if(vehicle_type = 'LIGHT_DUTY', 1, 0)) as "小型车流量",
SUM(if(vehicle_type = 'MEDIAN_SIZED', 1, 0)) as "中型车流量",
SUM(if(vehicle_type = 'OVERSIZE', 1, 0)) as "大车流量"
from
passing_vehicle
where
exist_time > 0
and time_stamp > toDateTime('2022-06-30 11:00:00')
and time_stamp < toDateTime('2022-06-30 15:00:00')
;
使用toStartOfInterval
select
toStartOfInterval(time_stamp , INTERVAL 30 minute) as half_hour,
count(1) as "总流量",
SUM(if(vehicle_type = 'PEDESTRIAN', 1, 0)) as "行人流量",
SUM(if(vehicle_type = 'NON_MOTORIZED', 1, 0)) as "非机动车流量",
SUM(if(vehicle_type = 'LIGHT_DUTY', 1, 0)) as "小型车流量",
SUM(if(vehicle_type = 'MEDIAN_SIZED', 1, 0)) as "中型车流量",
SUM(if(vehicle_type = 'OVERSIZE', 1, 0)) as "大车流量"
from
passing_vehicle
where
exist_time > 0
and time_stamp > toDateTime('2022-06-30 11:00:00')
and time_stamp < toDateTime('2022-06-30 15:00:00')
group by
half_hour,
order by
half_hour;
边栏推荐
- [100 cases of JVM tuning practice] 04 - Method area tuning practice (Part 1)
- pytorch之数据类型tensor
- taro3.*中使用 dva 入门级别的哦
- Part 7: STM32 serial communication programming
- MySQL中回表的代价
- Installation and testing of pyflink
- 如何管理分布式团队?
- tensorflow 1.14指定gpu运行设置
- Case development of landlord fighting game
- Lldp compatible CDP function configuration
猜你喜欢
[hfctf2020]babyupload session parsing engine
[Niuke] [noip2015] jumping stone
Telerik UI 2022 R2 SP1 Retail-Not Crack
Dell Notebook Periodic Flash Screen Fault
Let's see through the network i/o model from beginning to end
Lldp compatible CDP function configuration
Body mass index program, entry to write dead applet project
[Niuke] b-complete square
LLDP兼容CDP功能配置
迈动互联中标北京人寿保险,助推客户提升品牌价值
随机推荐
Building a dream in the digital era, the Xi'an station of the city chain science and Technology Strategy Summit ended smoothly
[hfctf2020]babyupload session parsing engine
Build your own website (17)
力扣1037. 有效的回旋镖
LLDP兼容CDP功能配置
Implementation principle of waitgroup in golang
Dell筆記本周期性閃屏故障
mysql: error while loading shared libraries: libtinfo.so.5: cannot open shared object file: No such
身体质量指数程序,入门写死的小程序项目
UI控件Telerik UI for WinForms新主题——VS2022启发式主题
The printf function is realized through the serial port, and the serial port data reception is realized by interrupt
Can the system hibernation file be deleted? How to delete the system hibernation file
STM32开发资料链接分享
Deep learning framework TF installation
《安富莱嵌入式周报》第272期:2022.06.27--2022.07.03
第四篇,STM32中断控制编程
Atomic in golang and CAS operations
How to evaluate load balancing performance parameters?
通过串口实现printf函数,中断实现串口数据接收
实现mysql与ES的增量数据同步