lgqwym 2022-10-18 11:40 采纳率: 76.9%
浏览 47
已结题

oracle求两个日期之间的日期

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

img

  • 写回答

4条回答 默认 最新

  • DarkAthena ORACLE应用及数据库设计方案咨询师 2022-10-18 13:03
    关注

    可以使用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;
    

    img


    connect by 的大概这样

    select distinct fstarttime, endtime, fstarttime + level - 1 dt
      from test_date_exp
    connect by    fstarttime + level - 1 <=  endtime
    order by 1,3
    

    img

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(3条)

报告相同问题?

问题事件

  • 系统已结题 10月26日
  • 已采纳回答 10月18日
  • 创建了问题 10月18日

悬赏问题

  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题
  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能
  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”