当前位置:网站首页>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;
边栏推荐
- Make Jar, Not War
- Atomic in golang, and cas Operations
- HMM 笔记
- [chip scheme design] pulse oximeter
- Docker method to install MySQL
- Grc: personal information protection law, personal privacy, corporate risk compliance governance
- Receive user input, height BMI, BMI detection small business entry case
- THREE.AxesHelper is not a constructor
- Spark TPCDS Data Gen
- Boot - Prometheus push gateway use
猜你喜欢
golang中的Mutex原理解析
c语言—数组
Yunna | work order management measures, how to carry out work order management
Asset security issues or constraints on the development of the encryption industry, risk control + compliance has become the key to breaking the platform
Data type of pytorch tensor
JTAG debugging experience of arm bare board debugging
Wood extraction in Halcon
【案例分享】网络环路检测基本功能配置
Typical problems of subnet division and super network construction
阿里云中mysql数据库被攻击了,最终数据找回来了
随机推荐
golang中的atomic,以及CAS操作
[chip scheme design] pulse oximeter
JTAG debugging experience of arm bare board debugging
Google发布安全更新,修复Chrome中已被利用的0 day
Make Jar, Not War
Machine learning: the difference between random gradient descent (SGD) and gradient descent (GD) and code implementation.
Make Jar, Not War
免费白嫖的图床对比
云呐-工单管理制度及流程,工单管理规范
[Niuke] b-complete square
Dynamic planning idea "from getting started to giving up"
NEON优化:矩阵转置的指令优化案例
Neon Optimization: performance optimization FAQ QA
Neon Optimization: an instruction optimization case of matrix transpose
如何管理分布式团队?
斗地主游戏的案例开发
Metauniverse urban legend 02: metaphor of the number one player
【案例分享】网络环路检测基本功能配置
JTAG principle of arm bare board debugging
机器学习:随机梯度下降(SGD)与梯度下降(GD)的区别与代码实现。