南强小屋 Design By 杰米

核心sql如下:

select daytime::date from generate_series(
('2017-06-01'),--查询开始日期(可根据需求调整) 
(select now()::date),--查询结束日期(可根据需求调整) 
 '1 day'--间隔(可根据需求调整)
) s(daytime)

以上sql,得到结果为从6月1号到今天这个时间区间内的每天的日期,如下:

postgresql 实现查询某时间区间的所有日期案例

举例说明:

查询tableA所有time_period区间内的日期,time_period的数据格式为:20170101-20170120;

select daytime,periods from (select daytime::date
from generate_series(
(select min(score_date) from tableA),--查询开始日期
(select now()::date),--查询结束日期
  '1 day'--间隔
) s(daytime)) t ,tableA where 
(t.daytime >((substr(time_period,0,5)||'-'||substr(time_period,5,2)||'-'||substr(time_period,7,2))::timestamp + '-1 day')::date ) 
and t.daytime <((substr(time_period,10,4)||'-'||substr(time_period,14,2)||'-'||substr(time_period,16,2))::timestamp + '1 day')::date

补充:PostgreSQL查找某个时间段内某条数据(订单)的最大创建时间、最小创建时间

sql语句:

select max(created_date),min(created_date) from biz_retail_order where source_order_no like '%daling_qa11184_%' and created_date > '2020-11-18 15:30:00' ;

结果为:

postgresql 实现查询某时间区间的所有日期案例

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。如有错误或未考虑完全的地方,望不吝赐教。

标签:
postgresql,查询时间区间,日期

南强小屋 Design By 杰米
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
南强小屋 Design By 杰米

评论“postgresql 实现查询某时间区间的所有日期案例”

暂无postgresql 实现查询某时间区间的所有日期案例的评论...