Hi all,
I am using SQL Server 2008 R2 and have a temp table with 3 columns: PersonID, EffectiveDate, and ExpirationDate. For each PersonID there are multiple rows with different values for the EffectiveDate and ExpirationDate columns I need one row for each person having an effective date more than one day after the expiration date of an earlier time period for that person (the business logic behind this is to find all customers with enrollment gaps).
Example data:
PersonID Eff Date Exp Date
1 1/1/1900 1/1/2001
1 1/1/1990 1/1/2005
1 7/1/2005 12/31/2013
1 1/1/2014 12/31/2020
2 1/1/1900 9/1/2013
2 9/2/2013 12/31/2015
Desired result set:
PersonID Eff Date Exp Date
1 7/1/2005 12/31/2015
Please note the PersonID values are not consecutive and there can be 2 or more rows in the table for each Person ID.
Any ideas how to do this? I have thought of comparing curr.EffDate > DATEADD(d, 1, prev.ExpDate) in some type of cursor or loop but can't seem to find a complete solution.
Thanks.
Scott Olander