当前位置:网站首页>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;
边栏推荐
- 【案例分享】网络环路检测基本功能配置
- Come on, don't spread it out. Fashion cloud secretly takes you to collect "cloud" wool, and then secretly builds a personal website to be the king of scrolls, hehe
- What are the differences between Oracle Linux and CentOS?
- Install Firefox browser on raspberry pie /arm device
- Oracle:CDB限制PDB资源实战
- Building a dream in the digital era, the Xi'an station of the city chain science and Technology Strategy Summit ended smoothly
- golang中的atomic,以及CAS操作
- Part VI, STM32 pulse width modulation (PWM) programming
- NEON优化:矩阵转置的指令优化案例
- Maidong Internet won the bid of Beijing life insurance to boost customers' brand value
猜你喜欢

第七篇,STM32串口通信编程

Dell筆記本周期性閃屏故障

Periodic flash screen failure of Dell notebook

Come on, don't spread it out. Fashion cloud secretly takes you to collect "cloud" wool, and then secretly builds a personal website to be the king of scrolls, hehe

Activereportsjs 3.1 Chinese version | | | activereportsjs 3.1 English version

Your cache folder contains root-owned files, due to a bug in npm ERR! previous versions of npm which
![Explain in detail the matrix normalization function normalize() of OpenCV [norm or value range of the scoped matrix (normalization)], and attach norm_ Example code in the case of minmax](/img/87/3fee9e6f687b0c3efe7208a25f07f1.png)
Explain in detail the matrix normalization function normalize() of OpenCV [norm or value range of the scoped matrix (normalization)], and attach norm_ Example code in the case of minmax

HMM 笔记

pytorch之数据类型tensor
![[100 cases of JVM tuning practice] 04 - Method area tuning practice (Part 1)](/img/7a/bd03943c39d3f731afb51fe2e0f898.png)
[100 cases of JVM tuning practice] 04 - Method area tuning practice (Part 1)
随机推荐
Openjudge noi 1.7 08: character substitution
The MySQL database in Alibaba cloud was attacked, and finally the data was found
golang中的Mutex原理解析
windows安装mysql8(5分钟)
Fastdfs data migration operation record
让我们,从头到尾,通透网络I/O模型
Meet in the middle
Informatics Olympiad YBT 1171: factors of large integers | 1.6 13: factors of large integers
Realize incremental data synchronization between MySQL and ES
In rails, when the resource creation operation fails and render: new is called, why must the URL be changed to the index URL of the resource?
Neon Optimization: About Cross access and reverse cross access
[case sharing] basic function configuration of network loop detection
Openjudge noi 1.7 10: simple password
JTAG principle of arm bare board debugging
「精致店主理人」青年创业孵化营·首期顺德场圆满结束!
table表格设置圆角
Analysis of mutex principle in golang
LLDP兼容CDP功能配置
批量获取中国所有行政区域经边界纬度坐标(到县区级别)
golang中的atomic,以及CAS操作