Oracle SQL问题:如何根据一张日期表创建时间段

huangapple go评论93阅读模式
英文:

Oracle SQL question: How to create periods based on a table of days

问题

2023-02-01 - 2023-02-03
2023-02-05 - 2023-02-09
2023-02-12 - 2023-02-28

英文:

I have a table Calendar that contains days.
If, as an example, the Calendar contains all days in the period 2023-02-01 to 2023-02-28 except the days 4, 10 and 11.

I would like to write a SQL that returns the three periods without the missing days like:<br/>
2023-02-01 - 2023-02-03<br/>
2023-02-05 - 2023-02-09<br/>
2023-02-12 - 2023-02-28

I write in the title that it is an Oracle question since I guess it is necessary to use analytical functions, but I cannot find out how.

Can anyone help me with that?

答案1

得分: 1

你可以尝试使用以下代码来获取实际间隔:

  1. with dates as (select trunc(sysdate,'yy')+level sd from dual connect by level<10
  2. union all
  3. select trunc(sysdate,'yy')+level+13 sd from dual connect by level<5
  4. union all
  5. select trunc(sysdate,'yy')+level+25 sd from dual connect by level<3
  6. )
  7. select *
  8. from (select sd, lag(sd) over (order by sd) lg
  9. from dates) match_recognize(
  10. order by sd
  11. measures first(sd) as start_sd, last(sd)+1 as end_sd
  12. one row per match
  13. pattern (e+)
  14. define e as sd=next(sd)-1);

结果如下:

  1. 02/01/2023 00:00:00 10/01/2023 00:00:00
  2. 15/01/2023 00:00:00 18/01/2023 00:00:00
  3. 27/01/2023 00:00:00 28/01/2023 00:00:00
英文:

You can try this to get the actual intervals

  1. with dates as (select trunc(sysdate,&#39;yy&#39;)+level sd from dual connect by level&lt;10
  2. union all
  3. select trunc(sysdate,&#39;yy&#39;)+level+13 sd from dual connect by level&lt;5
  4. union all
  5. select trunc(sysdate,&#39;yy&#39;)+level+25 sd from dual connect by level&lt;3
  6. )
  7. select *
  8. from (select sd, lag(sd) over (order by sd) lg
  9. from dates) match_recognize(
  10. order by sd
  11. measures first(sd) as start_sd, last(sd)+1 as end_sd
  12. one row per match
  13. pattern (e+)
  14. define e as sd=next(sd)-1);
  15. 02/01/2023 00:00:00 10/01/2023 00:00:00
  16. 15/01/2023 00:00:00 18/01/2023 00:00:00
  17. 27/01/2023 00:00:00 28/01/2023 00:00:00

答案2

得分: 0

我认为这应该有助于您获得您的结果

  1. SELECT
  2. prev_day, date_col
  3. FROM (
  4. SELECT
  5. date_col
  6. , LAG(date_col) OVER(ORDER BY date_col) prev_day
  7. FROM
  8. Calendar
  9. WHERE
  10. date_col BETWEEN sysdate - 6 AND sysdate -- 指定期间
  11. ) WHERE date_col - prev_day &gt; 1
英文:

I think this should help you to got your result

  1. SELECT
  2. prev_day, date_col
  3. FROM (
  4. SELECT
  5. date_col
  6. , LAG(date_col) OVER(ORDER BY date_col) prev_day
  7. FROM
  8. Calendar
  9. WHERE
  10. date_col BETWEEN sysdate - 6 AND sysdate -- spec period
  11. ) WHERE date_col - prev_day &gt; 1

huangapple
  • 本文由 发表于 2023年2月23日 20:16:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/75544715.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定