I have a table with millions of rows in it that require that date spans with a certain "contract number" and "service code" not overlap. There are typically 2-4 rows with dates that overlap that must be combined into a single record. Records which do not overlap should not be combined, and gaps should be maintained.
ContractNumber ServiceCode StartDate EndDate
1111111 AFL 2010-01-01 2010-12-31
1111111 AFL 2011-01-01 2011-12-31
1111111 AFL 2011-04-01 2012-03-31
1111111 AFL 2011-04-01 2012-06-30
1111111 AFL 2013-01-01 2013-12-31
Those must be combined into:
ContractNumber ServiceCode StartDate EndDate
1111111 AFL 2010-01-01 2010-12-31 (remains the same)
1111111 AFL 2011-01-01 2012-06-30 (merged)
1111111 AFL 2013-01-01 2013-12-31 (gap maintained)
Also, I have already looked at this question: Eliminate and reduce overlapping date ranges. It worked under a subset of data, but it was really slow once I unleashed it on all the data that I have. I need something that runs in a few minutes for millions of rows. Any ideas?