目录
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')