当前位置:网站首页>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;
边栏推荐
- 第七篇,STM32串口通信编程
- Gnet: notes on the use of a lightweight and high-performance go network framework
- Tencent cloud webshell experience
- Analysis of mutex principle in golang
- docker 方法安装mysql
- [batch dos-cmd command - summary and summary] - view or modify file attributes (attrib), view and modify file association types (Assoc, ftype)
- Neon Optimization: an optimization case of log10 function
- Summary of being a microservice R & D Engineer in the past year
- ARM裸板调试之JTAG调试体验
- Your cache folder contains root-owned files, due to a bug in npm ERR! previous versions of npm which
猜你喜欢

Maidong Internet won the bid of Beijing life insurance to boost customers' brand value
![[hfctf2020]babyupload session parsing engine](/img/db/6003129bc16f943ad9868561a2d5dc.png)
[hfctf2020]babyupload session parsing engine
![[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

pytorch之数据类型tensor

批量获取中国所有行政区域经边界纬度坐标(到县区级别)

Dynamic planning idea "from getting started to giving up"

Make a simple graphical interface with Tkinter

详解OpenCV的矩阵规范化函数normalize()【范围化矩阵的范数或值范围(归一化处理)】,并附NORM_MINMAX情况下的示例代码

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
![[HFCTF2020]BabyUpload session解析引擎](/img/db/6003129bc16f943ad9868561a2d5dc.png)
[HFCTF2020]BabyUpload session解析引擎
随机推荐
Receive user input, height BMI, BMI detection small business entry case
Installation and testing of pyflink
动态规划思想《从入门到放弃》
Maidong Internet won the bid of Beijing life insurance to boost customers' brand value
BFS realizes breadth first traversal of adjacency matrix (with examples)
【案例分享】网络环路检测基本功能配置
Windows installation mysql8 (5 minutes)
Telerik UI 2022 R2 SP1 Retail-Not Crack
HMM 笔记
Body mass index program, entry to write dead applet project
Tensorflow GPU installation
NEON优化:关于交叉存取与反向交叉存取
Deeply explore the compilation and pile insertion technology (IV. ASM exploration)
C # method of calculating lunar calendar date 2022
[HFCTF2020]BabyUpload session解析引擎
Can the system hibernation file be deleted? How to delete the system hibernation file
第四篇,STM32中断控制编程
Cause of handler memory leak
重上吹麻滩——段芝堂创始人翟立冬游记
Do you understand this patch of the interface control devaxpress WinForms skin editor?