全网搜索了很多相关的内容,大部分是mysql的语句,也没找到相关的存储过程,于是乎去国外的网站找了一下,还真找到了办法
国外参考网址:https://rob.conery.io/2018/08/01/simple-monthly-reports-in-postgresql-using-generate_series/
使用内置函数 generate_series
函数 | 参数类型 | 返回类型 | 描述 |
---|---|---|---|
generate_series(start, stop) | int 或 bigint | setof int 或 setof bigint(与参数类型相同) | 生成一个数值序列,从start 到 stop,步进为一 |
generate_series(start, stop, step) | int 或 bigint | setof int 或 setof bigint(与参数类型相同) | 生成一个数值序列,从start 到 stop,步进为step |
generate_series(start, stop, step_interval) | timestamp or timestamp with time zone | timestamp 或 timestamp with time zone(same as argument type) | 生成一个数值序列,从start 到 stop,步进为step |
遍历两个日期的每一天
select * from generate_series('2019-09-01 00:00:00'::TIMESTAMP, '2019-10-01 00:00:00'::TIMESTAMP, '1 day');
输出结果:
遍历两个日期的每小时
select * from generate_series('2019-09-01 00:00:00'::TIMESTAMP, '2019-10-01 00:00:00'::TIMESTAMP, '1 hour');
输出结果:
太强大了!!