postgresql 实现查询某时间区间的所有日期案例代替Oracle中的connect by

news/2024/7/9 23:32:17 标签: postgresql, oracle, 数据库
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' ;

结果为

 


http://www.niftyadmin.cn/n/1598467.html

相关文章

多种发动机,的机械原理图 动画图解

动画图解一般看不见的机械原理 动画图解一般看不见的机械原理——更新多款发动机&#xff0c;——现代生活离不开各种机械&#xff0c;无数复杂的机械走进了我们寻常百姓的生活中&#xff0c;小到我们家里客厅墙上的挂钟&#xff0c;大到出门上班用以代步的汽车&#xff0c;都…

PostgreSQL的generate_series函数应用产生一系列的填充数据。

一、简介 PostgreSQL 中有一个很有用处的内置函数generate_series&#xff0c;可以按不同的规则产生一系列的填充数据。 二、语法 函数参数类型返回类型描述generate_series(start, stop)int 或 bigintsetof int 或 setof bigint(与参数类型相同)生成一个数值序列&#xff0…

S3c2410 LINUX下如何访问IO端口

ARM体系结构中访问IO需使用驱动模块,可在模块初始化中使用 request_mem_region()函数申请IO内存,然后通过ioremap()函数对获得的IO内存进行重映射. 如控制s3c2410的LED显示,使用GPF口,可用如下代码段进行访问#define GPF 0x56000000...static void * GPF_BASE; //一定要定义成v…

Postgresql 类似oracle的NVL方法

oracle 的NVL(col,0)是判断如果col字段为空的时候赋值0。 postgresql里也有类似的方法COALESCE&#xff0c; COALESCE函数是返回参数中的第一个非null的值&#xff0c;它要求参数中至少有一个是非null的&#xff0c;如果参数都是null会报错&#xff0c;如下 SELECT coalesce(…

javabean 复习

jsp代码&#xff1a; <% page contentType"text/html;charsetGBK" import"java.sql.*" %> <jsp:useBean id"jb" scope"page" class"bean.JavaBeanJSP"/> <%! ResultSet rs; %> <html> <head>…

pg数据库获取前几条记录

select id , date_id , namefrom (select * , row_number() over(partition by date_id order by id ) as row_id from dddd ) as twhere t.row_id < 2 ; row_number() over(partition by date_id order by id )

Linux设备驱动开发环境的搭建

刚接触Linux设备驱动时&#xff0c;初学者往往连如何编译驱动程序都不懂&#xff0c;更别说编译进内核或加载测试了。一般都是在网上找个最简单的 helloworld驱动程序&#xff0c;然后严格按照网上所说的步骤编译&#xff0c;结果却得到一大堆见都没见过的错误&#xff0c;更不…

postgresql行号类似oracle rownum效果

使用窗口函数row_number() select row_number() OVER (ORDER BY id) as rownum ,* from score; 如果不关心排序&#xff0c;可以这样select row_number() over() as rownum,* from score; ———————————————— 版权声明&#xff1a;本文为CSDN博主「tanweii163」…