oracle数据库表中有开始时间和结束时间两个字段,有多条数据,怎么求开始时间,结束时间和之间的日期呢,注意:可能有多条数据,如图:

oracle求两个日期之间的日期
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
4条回答 默认 最新
关注
可以使用connect by 或者 cte两种方式,下面这个是cte的方式(注:因为不清楚fstarttime是否会有重复值,因此引入了行号来做行的唯一标识)
--测试数据 create table test_date_exp(fstarttime date,endtime date); insert into test_date_exp values (date'2022-10-01',date'2022-10-07'); insert into test_date_exp values (date'2022-10-16',date'2022-10-18'); --执行查询 with t as (select rownum rn, t.* from test_date_exp t), cte(rn,fstarttime,endtime,lvl,dt) as (select rn, fstarttime, endtime, 1, fstarttime from t union all select a.rn, a.fstarttime, a.endtime, lvl + 1, a.fstarttime + lvl from cte, t a where a.endtime >= a.fstarttime + lvl and cte.rn = a.rn) select * from cte order by rn,dt;
connect by 的大概这样
select distinct fstarttime, endtime, fstarttime + level - 1 dt from test_date_exp connect by fstarttime + level - 1 <= endtime order by 1,3
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报 编辑记录