当前位置:网站首页>Clickhouse fields are grouped and aggregated, and SQL is queried according to the granularity of any time period
Clickhouse fields are grouped and aggregated, and SQL is queried according to the granularity of any time period
2022-07-07 01:26:00 【Persistence is an attitude】
ClickHouse Field grouping aggregation 、 Query according to the granularity of any time period SQL

demand
- Basic data warehousing , You need to do some aggregation according to the fields
- Existing intersection passing data , There are various models , It is necessary to count the total flow and the flow of each model , To return together
- Existing passing data , It needs different granularity according to time , Count the traffic flow , Achieve different time granularity summation ,5 minute 、10 minute 、 Half an hour 、 A month 、 A year will do
solve
Use if and sum
- Mainly used
if
, The eligible conditions are 1, The nonconformity is 0, Reusesum
Sum up
select
count(1) as " Total discharge ",
SUM(if(vehicle_type = 'PEDESTRIAN', 1, 0)) as " Pedestrian flow ",
SUM(if(vehicle_type = 'NON_MOTORIZED', 1, 0)) as " Non motor vehicle flow ",
SUM(if(vehicle_type = 'LIGHT_DUTY', 1, 0)) as " Small traffic flow ",
SUM(if(vehicle_type = 'MEDIAN_SIZED', 1, 0)) as " Medium traffic flow ",
SUM(if(vehicle_type = 'OVERSIZE', 1, 0)) as " Large traffic flow "
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')
;
Use toStartOfInterval
select
toStartOfInterval(time_stamp , INTERVAL 30 minute) as half_hour,
count(1) as " Total discharge ",
SUM(if(vehicle_type = 'PEDESTRIAN', 1, 0)) as " Pedestrian flow ",
SUM(if(vehicle_type = 'NON_MOTORIZED', 1, 0)) as " Non motor vehicle flow ",
SUM(if(vehicle_type = 'LIGHT_DUTY', 1, 0)) as " Small traffic flow ",
SUM(if(vehicle_type = 'MEDIAN_SIZED', 1, 0)) as " Medium traffic flow ",
SUM(if(vehicle_type = 'OVERSIZE', 1, 0)) as " Large traffic flow "
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;
边栏推荐
猜你喜欢
C language - array
Yunna | work order management measures, how to carry out work order management
[Niuke] b-complete square
Tensorflow GPU installation
boot - prometheus-push gateway 使用
云呐-工单管理制度及流程,工单管理规范
Asset security issues or constraints on the development of the encryption industry, risk control + compliance has become the key to breaking the platform
Yunna | work order management software, work order management software app
MySQL script batch queries all tables containing specified field types in the database
系统休眠文件可以删除吗 系统休眠文件怎么删除
随机推荐
Yunna | work order management software, work order management software app
Dynamic planning idea "from getting started to giving up"
Byte P7 professional level explanation: common tools and test methods for interface testing, Freeman
[signal and system]
Force buckle 1037 Effective boomerang
字节P7专业级讲解:接口测试常用工具及测试方法,福利文
数据手册中的词汇
Realize incremental data synchronization between MySQL and ES
C语言实例_5
Dark horse notes - create immutable sets and streams
Neon Optimization: an optimization case of log10 function
Go zero micro service practical series (IX. ultimate optimization of seckill performance)
LLDP兼容CDP功能配置
从零开始匹配vim(0)——vimscript 简介
Js逆向——捅了【马蜂窝】的ob混淆与加速乐
AI automatically generates annotation documents from code
阿里云中mysql数据库被攻击了,最终数据找回来了
一起看看matlab工具箱内部是如何实现BP神经网络的
Yunna - work order management system and process, work order management specification
Gnet: notes on the use of a lightweight and high-performance go network framework