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!