I'm am trying to find a way to compare a serives of data ranges to find gaps, however i need to exclude date ranges that are wholly within another range. Some example data:
PERSON_ID START_DATE END_DATE
0001 01/05/2014 30/11/2014
0001 01/06/2014 01/08/2014
0001 01/07/2014 01/11/2014
0001 01/12/2014 31/03/2015
I know that i can use the LEAD function to compare one line to the next to see where the gap is if there is one, e.g.:
SELECT END_DATE
FROM
(SELECT t.*,
lead(START_DATE,1) OVER (ORDER BY START_DATE) AS next_date
FROM table t
)
WHERE END_DATE+1<>next_date;
The issue is that this would bring back a false positive. The second and third rows of date ranges are wholly contained within the first and therefore should not be included in the gap calculations. I know i need to amend the offset argument in the LEAD function but i'm not sure of an efficient way of doing this for hundreds of person ids. Any thoughts?