根据 SQL 中预定义的日期范围对记录进行分组 (Oracle)

Group Records based on predefined date range in SQL (Oracle)

是否可以根据行的 start_date 和前一行的 end_date 对非连续日期的预定义日期范围差异(例如 30 天)对记录进行分组?我想取每组的min(start_date)max(end_date)。我在 Oracle 中尝试使用 leadlag 函数进行分区,但没有找到合适的解决方案。可以找到与我的问题相关但未回答的 post here

例如

ROW_NUM        PROJECT_ID       START_DATE       END_DATE          
1              1                2016-01-14       2016-08-15
2              1                2016-08-16       2016-09-10     --- Date diff Row 1&2 = 1 Day
3              1                2016-11-15       2017-01-10     --- Date diff Row 2&3  = 66 Days
4              1                2016-01-17       2017-04-10     --- Date diff Row 3&4 = 7 Days
5              2                2018-04-28       2018-06-01     --- Other Project
6              2                2019-02-01       2019-04-05     --- Diff > 30 Days
7              2                2019-04-08       2019-07-28     --- Diff 3 Days

预期结果:

ROW_NUM        PROJECT_ID       START_DATE       END_DATE          
1              1                2016-01-14       2016-09-10
3              1                2016-11-15       2017-04-10     
5              2                2018-04-28       2018-06-01   
6              2                2019-02-01       2019-07-28

     

使用 lag() 和累积总和来定义组的开始位置。然后汇总:

select project_id, min(start_date), max(end_date)
from (select t.*,
             sum(case when prev_end_date > start_date - interval '30' day then 0 else 1 end) over
                  (partition by project_id order by start_date) as grp
      from (select t.*,
                   lag(end_date) over (partition by project_id order by start_date) as prev_end_date
            from t
           ) t
     ) t
group by project_id, grp;