当前位置:网站首页>[postgresql]postgresqlg使用enerate_series() 函数补全统计
[postgresql]postgresqlg使用enerate_series() 函数补全统计
2022-07-26 22:12:00 【just-do-it-zzj】
目录
1.需求
比如高速公路的某个门架的过车明细数据如下:

需求:
需要按天统计通过该门架的过车总数量,没有过车的时候统计为0,即7月13日没有记录也要在报表中表示为0
with t1 as
(
select generate_series('2022-07-10'::date,'2022-07-17'::date,'1 day') as day
),
t2 (
select
pass_id,
capture_time,
to_char(capture_time,'yyyy-mm-dd')::date as capture_date,
grantry_code,
from sdi_op_toll_station_pass_detail_t
)
select day,grantry_code,count(pass_id) as cnt
from t1 left join t2
on t1.day=t2.capture_date
group by day,grantry_code;
2.generate_series函数
| 函数 | 参数类型 | 返回类型 | 描述 |
| generate_series(start, stop) | int 或 bigint | int 或 bigint(与参数类型相同) | 生成一个数值序列,从start 到 stop,步进为一 |
| generate_series(start, stop, step) | int 或 bigint | int 或 bigint(与参数类型相同) | 生成一个数值序列,从start 到 stop,步进为step |
| generate_series(start, stop, step_interval) | timestamp or timestamp with time zone | timestamp 或 timestamp with time zone(与参数类型相同) | 生成一个数值序列,从start 到 stop,步进为step |
3.实例
3.1 间隔1天
select generate_series('2022-07-10'::date,'2022-07-17'::date,'1 day')

3.2 间隔1小时
select generate_series('2022-07-10'::date,'2022-07-17'::date,'1 hour')

3.3 间隔1分钟
select generate_series('2022-07-10'::date,'2022-07-17'::date,'1 min')

边栏推荐
- what is qrc in qt
- 商汤科技CEO徐立:公司估值已超70亿美元,不急于上市
- NVIDIA SMI error: NVIDIA SMI has failed because it could't communicate with the NVIDIA driver complete record
- Xu Li, CEO of Shangtang Technology: the company's valuation has exceeded $7billion, so we are not in a hurry to go public
- [MySQL] - index principle and use
- New thrust of Moore's law, detailed explanation of Intel Advanced Packaging Technology!
- 华为Atlas900揭秘:集成数千颗昇腾910芯片,算力堪比50万台PC!
- 云原生微服务第一章之服务器环境说明
- App information reconnaissance & night God simulator burp packet capture configuration
- Blog Garden beautification skills summary
猜你喜欢

gateway基本使用

DTS is equipped with a new self-developed kernel, which breaks through the key technology of the three center architecture of the two places Tencent cloud database

Implementation of V-model syntax sugar

7.27抢先看 | openEuler 志高远,开源汇智创未来-开放原子全球开源峰会欧拉分论坛最详细议程出炉

Regular expressions and bypass case recurrence

Introduction to MySQL database

Apifox -- a better API testing tool than postman

DAO:OP 代币和不可转让的 NFT 致力于建立新的数字民主

Makefile related syntax summary (openc910)

Interview: your most impressive bug, for example
随机推荐
[MySQL] - index principle and use
【HCIP】OSPF 关系建立
systemctl命令
DTS搭载全新自研内核,突破两地三中心架构的关键技术|腾讯云数据库
What if redis memory is full? This is the right way to deal with it
程序员成长第二十九篇:如何激励员工?
顺序表实现
面试官问:JS的this指向
Shardingsphere JDBC keyword problem
Xinding acquires Ziguang holdings! Wanye enterprise: comprehensive transformation of integrated circuits!
Systemctl command
KT6368A蓝牙芯片开发注意事项以及问题集锦--长期更新
Parameter analysis and stone jumping board
正则表达式与绕过案例复现
缓存数据库Memcached
[hcip] OSPF special area, summary, certification
[hcip] OSPF route calculation
Weilai cup 2022 Niuke summer multi school training camp 2
三星Galaxy Z可折叠产品的预告片泄露:'柔性好于平面'
华为Atlas900揭秘:集成数千颗昇腾910芯片,算力堪比50万台PC!