2

如果第二行的开始是第一行结束后的第二天,我的问题是加入更多的日期间隔。在所有这些情况下,我应该只有一排。前任:

id      start           end
10      20120101        20120125
10      20120201        20120225
10      20120226        20120302
10      20120303        20120304

    

一定是

10      20120101        20120125
10      20120201        20120304

你有什么建议吗?

4

1 回答 1

3

这是我的建议:您是否在表中有这些行,我们称之为“temp_table”,您可以创建一个仅保留相关日期的视图:

CREATE VIEW temp_view AS
SELECT id, dd, ROWNUM AS nr
  FROM (
    SELECT s.ID, s.start_date AS dd --for izolated intervals, keep the wtart date
      FROM temp_table s, temp_table prv, temp_table nxt
     WHERE prv.end_date(+) = s.start_date-1 AND prv.ID(+) = s.ID
       AND s.end_date+1 = nxt.start_date(+) AND nxt.ID(+) = s.ID
       AND prv.ID IS NULL AND nxt.ID IS NULL
    UNION
    SELECT s.ID, decode(nxt.id, 
                   NULL, s.end_date, --no next interval => keep the end date
                   decode(prv.id,
                      NULL, s.start_date)  --no previous interval => keep the start date
                  ) AS dd 
      FROM temp_table s, temp_table prv, temp_table nxt
     WHERE prv.end_date(+) = s.start_date-1 AND prv.ID(+) = s.ID
       AND s.end_date+1 = nxt.start_date(+) AND nxt.ID(+) = s.ID
       AND (prv.ID IS NULL OR nxt.ID IS NULL)
    ORDER BY dd   --order all the dates ascending  
  ); 

此视图将返回如下内容:

ID  DD          NR
10  20120101    1
10  20120125    2
10  20120201    3
10  20120304    4

您可以查询如下内容:

SELECT start_dates.ID, start_dates.dd AS start_date, end_dates.dd AS end_date
  FROM temp_view start_dates, temp_view end_dates 
 WHERE start_dates.ID = end_dates.ID
   AND start_dates.nr+1 = end_dates.nr 
   AND MOD(start_dates.nr,2) = 1 

结果:

ID  START_DATE  END_DATE
10  20120101    20120125
10  20120201    20120304
于 2012-11-02T09:02:29.377 回答