Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare dates in different columns and rows dynamically in SQL

Tags:

sql

ssms

t-sql

I have a set of data like this.

Data

ID  Start_dt    End_dt
A   1/1/2010    12/31/2010
A   1/1/2011    12/31/2011
A   6/1/2012    12/31/2012
A   1/1/2014    12/31/2014
A   1/1/2016    10/31/2016
A   1/1/2018    12/31/2018
B   1/1/2016    2/29/2016
B   3/1/2016    10/31/2016
B   1/1/2017    7/31/2017
B   1/1/2019    12/31/9999
C   1/1/2017    12/31/2017
C   1/1/2017    12/31/2018
C   1/1/2019    12/31/9999

I need to create a query that looks at each member's row, compares the current Start_dt against the previous End_dt. If the difference is less than one year, treat those 2 records as one continuous enrollment and return the combined MIN Start_dt and MAX End_dt, and repeat that for all rows for each member. If the difference is >=1 year, treat that as separate enrollment.

Desired result

ID  Start_dt    End_dt
A   1/1/2010    12/31/2012
A   1/1/2014    12/31/2014
A   1/1/2016    10/31/2016
A   1/1/2018    12/31/2018
B   1/1/2016    7/31/2017
B   1/1/2019    12/31/2019
C   1/1/2017    12/31/9999

Here's a Create Table query:

if OBJECT_ID ('tempdb..#test1') is not null
drop table #test1
CREATE TABLE #test1 (
    ID varchar(10),
    Start_dt datetime,
    End_dt datetime
);

INSERT INTO #test1 VALUES ('A', '1/1/2010', '12/31/2010')
,('A', '1/1/2011', '12/31/2011')
,('A', '6/1/2012', '12/31/2012')
,('A', '1/1/2014', '12/31/2014')
,('A', '1/1/2016', '10/31/2016')
,('A', '1/1/2018', '12/31/2018')

,('B', '1/1/2016', '2/29/2016')
,('B', '3/1/2016', '10/31/2016')
,('B', '1/1/2017', '7/31/2017')
,('B', '1/1/2019', '12/31/9999')

,('C', '1/1/2017', '12/31/2017')
,('C', '1/1/2017', '12/31/2018')
,('C', '1/1/2019', '12/31/2999')

I've been trying to solve this for days but have tried self-joins, loops but have not found a good solution. Can someone help?

Thank you!

like image 942
Henry Avatar asked Dec 17 '25 05:12

Henry


1 Answers

You can use lag() or a cumulative max() to get the previous end date. Then compare it to the current start date.

When the difference is more than a year, then a new group starts. Do a cumulative sum of these new group starts to get a grouping id.

And the rest is aggregation:

select id, min(start_dt), max(end_dt)
from (select t1.*,
             sum(case when prev_end_dt > dateadd(year, -1, start_dt) then 0 else 1 end) over
                 (partition by id order by start_dt) as grp
      from (select t1.*,
                   max(end_dt) over (partition by id
                                      order by start_dt
                                      rows between unbounded preceding and 1 preceding
                                     ) as prev_end_dt
            from test1 t1
           ) t1
     ) t1
group by id, grp
order by id, min(start_dt);
like image 145
Gordon Linoff Avatar answered Dec 19 '25 21:12

Gordon Linoff



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!