Condense Time Periods with SQL

前端 未结 4 1552
春和景丽
春和景丽 2021-02-20 17:41

I have a large data set which for the purpose of this question has 3 fields:

  • Group Identifier
  • From Date
  • To Date

On any given row t

4条回答
  •  谎友^
    谎友^ (楼主)
    2021-02-20 18:38

    The solution from book "Microsoft® SQL Server ® 2012 High-Performance T-SQL Using Window Functions"

    ;with C1 as(
    select GroupID, FromDate as ts, +1 as type, 1 as sub
      from dbo.table_name
    union all
    select GroupID, dateadd(day, +1, ToDate) as ts, -1 as type, 0 as sub
      from dbo.table_name),
    C2 as(
    select C1.*
         , sum(type) over(partition by GroupID order by ts, type desc
                          rows between unbounded preceding and current row) - sub as cnt
      from C1),
    C3 as(
    select GroupID, ts, floor((row_number() over(partition by GroupID order by ts) - 1) / 2 + 1) as grpnum
      from C2
      where cnt = 0)
    
    select GroupID, min(ts) as FromDate, dateadd(day, -1, max(ts)) as ToDate
      from C3
      group by GroupID, grpnum;
    

    Create table:

    if object_id('table_name') is not null
      drop table table_name
    create table table_name(GroupID varchar(100), FromDate datetime,ToDate datetime)
    insert into table_name
    select 'A', '01/01/2012', '12/31/2012' union all
    select 'A', '12/01/2013', '11/30/2014' union all
    select 'A', '01/01/2015', '12/31/2015' union all
    select 'A', '01/01/2015', '12/31/2015' union all
    select 'A', '02/01/2015', '03/31/2015' union all
    select 'A', '01/01/2013', '12/31/2013'
    

提交回复
热议问题