如何使用sql对同一范围内的多个记录的开始日期和结束日期进行分组

时间:2019-09-11 23:57:02

标签: sql group-by rank

我有一个查询,向我返回一些记录,如下所示:

ip; start_date; end_date
0.0.0.0; 09/10/2018 00: 00: 00; 10/10/2018 01:00:00
0.0.0.0; 10/10/2018 00: 00: 00; 11/10/2018 01:00:00
0.0.0.0; 10/10/2018 00: 00: 00; 11/10/2018 02:00:00
0.0.0.0; 10/10/2018 00: 00: 00; 11/10/2018 03:00:00
0.0.0.0; 10/10/2018 00: 00: 00; 11/10/2018 05:00:00
0.0.0.0; 10/12/2018 00: 00: 00; 10/10/2018 04:00:00

如您所见,我有一些记录,如果我们按开始日期和结束日期进行分组,它们将在同一时期内;

如何对查询进行分组以获取记录,如下所示:

0.0.0.0; 09/10/2018 00: 00: 00; 11/10/2018 05:00:00
0.0.0.0; 10/12/2018 00: 00: 00; 10/10/2018 04:00:00

如您所见,前5行包含在同一范围内,而最后5行不在同一范围内。

1 个答案:

答案 0 :(得分:0)

您可以使用累积的max()来查看重叠的位置。没有重叠的地方,则开始一组。开始的累积总和决定了每个“岛”,最后一步是聚合:

select ip, grp,
       min(start_date), max(end_date)
from (select t.*,
             sum(case when prev_max_end_date >= start_date
                      then 0 else 1
                 end) over (partition by ip order by start_date) as grp
      from (select t.*,
                   max(end_date) over (partition by ip 
                                       order by start_date
                                       rows between unbounded preceding and 1 preceding
                                      ) as prev_max_end_date
            from t
           ) t
      ) t
group by ip, grp;