怎么用SQL求一个日期区间

怎么用SQL求一个日期区间

SQL 我有一只绘梦笔 发布于 2023-7-13 09:51 (编辑于 2023-7-13 21:58)
1min目标场景问卷 立即参与
回答问题
悬赏:0 F币 + 添加悬赏
提示:增加悬赏、完善问题、追问等操作,可使您的问题被置顶,并向所有关注者发送通知
共1回答
最佳回答
0
我有一只绘梦笔Lv4见习互助
发布于2023-7-13 09:57
  1. with gpdata as

  2. (select 1 xuhao, '股A' gp, to_date('20200301', 'yyyyMMdd') rq

  3. from dual

  4. union all

  5. select 2 xuhao, '股A' gp, to_date('20200302', 'yyyyMMdd') rq

  6. from dual

  7. union all

  8. select 3 xuhao, '股A' gp, to_date('20200303', 'yyyyMMdd') rq

  9. from dual

  10. union all

  11. select 4 xuhao, '股A' gp, to_date('20200305', 'yyyyMMdd') rq

  12. from dual

  13. union all

  14. select 5 xuhao, '股A' gp, to_date('20200306', 'yyyyMMdd') rq

  15. from dual

  16. union all

  17. select 6 xuhao, '股A' gp, to_date('20200309', 'yyyyMMdd') rq

  18. from dual

  19. union all

  20. select 7 xuhao, '股B' gp, to_date('20200302', 'yyyyMMdd') rq

  21. from dual

  22. union all

  23. select 8 xuhao, '股B' gp, to_date('20200303', 'yyyyMMdd') rq

  24. from dual

  25. union all

  26. select 9 xuhao, '股B' gp, to_date('20200304', 'yyyyMMdd') rq

  27. from dual

  28. union all

  29. select 10 xuhao, '股B' gp, to_date('20200305', 'yyyyMMdd') rq

  30. from dual

  31. union all

  32. select 11 xuhao, '股B' gp, to_date('20200308', 'yyyyMMdd') rq

  33. from dual

  34. union all

  35. select 12 xuhao, '股B' gp, to_date('20200309', 'yyyyMMdd') rq

  36. from dual

  37. union all

  38. select 13 xuhao, '股B' gp, to_date('20200311', 'yyyyMMdd') rq

  39. from dual)

  40. select gp 股票,

  41. to_char(min(rq), 'yyyyMMdd') 开始日期,

  42. to_char(max(rq), 'yyyyMMdd') 结束日期,

  43. max(rq) - min(rq) + 1 持续天数

  44. from gpdata t

  45. start with not exists (select 1

  46. from gpdata

  47. where gp = t.gp

  48. and rq = t.rq - 1)

  49. connect by prior gp = gp

  50. and prior rq = rq - 1

  51. group by gp, connect_by_root rq

  52. having count (1) > 0

  53. order by 1, 2;

  • 0关注人数
  • 229浏览人数
  • 最后回答于:2023-7-13 21:58
    请选择关闭问题的原因
    确定 取消
    返回顶部