Joining together consecutive date validity intervals

后端 未结 4 2053
予麋鹿
予麋鹿 2021-01-14 02:59

I have a series of records containing some information (product type) with temporal validity.

I would like to meld together adjacent validity intervals, provided tha

4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-14 03:25

    This is a gaps-and-islands problem. There are various ways to approach it; this uses lead and lag analytic functions:

    select distinct product,
      case when start_date is null then lag(start_date)
        over (partition by product order by rn) else start_date end as start_date,
      case when end_date is null then lead(end_date)
        over (partition by product order by rn) else end_date end as end_date
    from (
      select product, start_date, end_date, rn
      from (
        select t.product,
          case when lag(end_date)
              over (partition by product order by start_date) is null
            or lag(end_date)
              over (partition by product order by start_date) != start_date - 1
            then start_date end as start_date,
          case when lead(start_date)
              over (partition by product order by start_date) is null
            or lead(start_date)
              over (partition by product order by start_date) != end_date + 1
            then end_date end as end_date,
          row_number() over (partition by product order by start_date) as rn
        from t
      )
      where start_date is not null or end_date is not null
    )
    order by start_date, product;
    
    PRODUCT START_DATE END_DATE
    ------- ---------- ---------
    A       01-JUL-13  30-SEP-13 
    B       01-OCT-13  30-NOV-13 
    A       01-DEC-13  31-MAR-14 
    

    SQL Fiddle

    The innermost query looks at the preceding and following records for the product, and only retains the start and/or end time if the records are not contiguous:

    select t.product,
      case when lag(end_date)
          over (partition by product order by start_date) is null
        or lag(end_date)
          over (partition by product order by start_date) != start_date - 1
        then start_date end as start_date,
      case when lead(start_date)
          over (partition by product order by start_date) is null
        or lead(start_date)
          over (partition by product order by start_date) != end_date + 1
        then end_date end as end_date
    from t;
    
    PRODUCT START_DATE END_DATE
    ------- ---------- ---------
    A       01-JUL-13            
    A                            
    A                  30-SEP-13 
    A       01-DEC-13            
    A                            
    A                            
    A                  31-MAR-14 
    B       01-OCT-13            
    B                  30-NOV-13 
    

    The next level of select removes those which are mid-period, where both dates were blanked by the inner query, which gives:

    PRODUCT START_DATE END_DATE
    ------- ---------- ---------
    A       01-JUL-13            
    A                  30-SEP-13 
    A       01-DEC-13            
    A                  31-MAR-14 
    B       01-OCT-13            
    B                  30-NOV-13 
    

    The outer query then collapses those adjacent pairs; I've used the easy route of creating duplicates and then eliminating them with distinct, but you can do it other ways, like putting both values into one of the pairs of rows and leaving both values in the other null, and then eliminating those with another layer of select, but I think distinct is OK here.

    If your real-world use case has times, not just dates, then you'll need to adjust the comparison in the inner query; rather than +/- 1, an interval of 1 second perhaps, or 1/86400 if you prefer, but depends on the precision of your values.

提交回复
热议问题