当前位置:网站首页>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;
边栏推荐
- windows安装mysql8(5分钟)
- 第三方跳转网站 出现 405 Method Not Allowed
- How to evaluate load balancing performance parameters?
- JTAG debugging experience of arm bare board debugging
- 接收用户输入,身高BMI体重指数检测小业务入门案例
- [batch dos-cmd command - summary and summary] - string search, search, and filter commands (find, findstr), and the difference and discrimination between find and findstr
- Openjudge noi 1.7 08: character substitution
- Let's see through the network i/o model from beginning to end
- 系统休眠文件可以删除吗 系统休眠文件怎么删除
- NEON优化:性能优化经验总结
猜你喜欢

Part IV: STM32 interrupt control programming

pytorch之数据类型tensor

Body mass index program, entry to write dead applet project

系统休眠文件可以删除吗 系统休眠文件怎么删除
![[Batch dos - cmd Command - Summary and Summary] - String search, find, Filter Commands (FIND, findstr), differentiation and Analysis of Find and findstr](/img/4a/0dcc28f76ce99982f930c21d0d76c3.png)
[Batch dos - cmd Command - Summary and Summary] - String search, find, Filter Commands (FIND, findstr), differentiation and Analysis of Find and findstr
![[batch dos-cmd command - summary and summary] - string search, search, and filter commands (find, findstr), and the difference and discrimination between find and findstr](/img/4a/0dcc28f76ce99982f930c21d0d76c3.png)
[batch dos-cmd command - summary and summary] - string search, search, and filter commands (find, findstr), and the difference and discrimination between find and findstr
![[牛客] [NOIP2015]跳石头](/img/9f/b48f3c504e511e79935a481b15045e.png)
[牛客] [NOIP2015]跳石头
![[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)

Dynamic planning idea "from getting started to giving up"

Transformation transformation operator
随机推荐
Dell筆記本周期性閃屏故障
动态规划思想《从入门到放弃》
Niuke cold training camp 6B (Freund has no green name level)
[HFCTF2020]BabyUpload session解析引擎
ZABBIX 5.0: automatically monitor Alibaba cloud RDS through LLD
Lldp compatible CDP function configuration
NEON优化:性能优化经验总结
paddlehub应用出现paddle包报错的问题
Anfulai embedded weekly report no. 272: 2022.06.27--2022.07.03
阿里云中mysql数据库被攻击了,最终数据找回来了
from . cv2 import * ImportError: libGL. so. 1: cannot open shared object file: No such file or direc
详解OpenCV的矩阵规范化函数normalize()【范围化矩阵的范数或值范围(归一化处理)】,并附NORM_MINMAX情况下的示例代码
Make a simple graphical interface with Tkinter
STM32开发资料链接分享
View remote test data and records anytime, anywhere -- ipehub2 and ipemotion app
界面控件DevExpress WinForms皮肤编辑器的这个补丁,你了解了吗?
[牛客] [NOIP2015]跳石头
Part 7: STM32 serial communication programming
Installation and testing of pyflink
如何管理分布式团队?