• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

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

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

核心sql如下:

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

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

举例说明:

查询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' ;

结果为:

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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Oracle 表空间查询与操作方法发布时间:2022-02-08
下一篇:
MySQL数据库优化之索引实现原理与用法分析发布时间:2022-02-08
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap