当前位置:网站首页>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;
边栏推荐
- Neon Optimization: summary of performance optimization experience
- Gnet: notes on the use of a lightweight and high-performance go network framework
- Activereportsjs 3.1 Chinese version | | | activereportsjs 3.1 English version
- NEON优化:性能优化经验总结
- Part V: STM32 system timer and general timer programming
- [batch dos-cmd command - summary and summary] - string search, search, and filter commands (find, findstr), and the difference and discrimination between find and findstr
- UI control telerik UI for WinForms new theme - vs2022 heuristic theme
- 实现mysql与ES的增量数据同步
- LLDP兼容CDP功能配置
- [Niuke] [noip2015] jumping stone
猜你喜欢

省市区三级坐标边界数据csv转JSON
![[牛客] B-完全平方数](/img/bd/0812b4fb1c4f6217ad5a0f3f3b8d5e.png)
[牛客] B-完全平方数
![[100 cases of JVM tuning practice] 05 - Method area tuning practice (Part 2)](/img/40/dc45df3cd3ee7642277eff899bc6aa.png)
[100 cases of JVM tuning practice] 05 - Method area tuning practice (Part 2)

JTAG principle of arm bare board debugging

View remote test data and records anytime, anywhere -- ipehub2 and ipemotion app
![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

Periodic flash screen failure of Dell notebook

Boot - Prometheus push gateway use

"Exquisite store manager" youth entrepreneurship incubation camp - the first phase of Shunde market has been successfully completed!
![[batch dos-cmd command - summary and summary] - jump, cycle, condition commands (goto, errorlevel, if, for [read, segment, extract string]), CMD command error summary, CMD error](/img/a5/41d4cbc070d421093323dc189a05cf.png)
[batch dos-cmd command - summary and summary] - jump, cycle, condition commands (goto, errorlevel, if, for [read, segment, extract string]), CMD command error summary, CMD error
随机推荐
"Exquisite store manager" youth entrepreneurship incubation camp - the first phase of Shunde market has been successfully completed!
How to evaluate load balancing performance parameters?
The difference between spin and sleep
Data type of pytorch tensor
golang中的atomic,以及CAS操作
The MySQL database in Alibaba cloud was attacked, and finally the data was found
Installation and testing of pyflink
Oracle:CDB限制PDB资源实战
[100 cases of JVM tuning practice] 05 - Method area tuning practice (Part 2)
Boot - Prometheus push gateway use
Receive user input, height BMI, BMI detection small business entry case
Transformation transformation operator
Dell筆記本周期性閃屏故障
Part IV: STM32 interrupt control programming
Part V: STM32 system timer and general timer programming
SuperSocket 1.6 创建一个简易的报文长度在头部的Socket服务器
Taro2.* 小程序配置分享微信朋友圈
from .cv2 import * ImportError: libGL.so.1: cannot open shared object file: No such file or direc
ARM裸板调试之JTAG原理
Neon Optimization: an optimization case of log10 function