I have a set of date ranges consisting of both partially and fully overlapping dates, like this:
UserID StartDate EndDate
====== ========== ==========
1 2011-01-01 2011-01-02 <- A
1 2011-01-01 2011-01-10 <- A
1 2011-01-08 2011-02-15 <- A
1 2011-02-20 2011-03-10 <- B
2 2011-01-01 2011-01-20 <- C
2 2011-01-15 2011-01-25 <- C
Using T-SQL, I would like to create a new set of data, per user, with eliminated overlapping data, extending ranges and removing redundant data where needed, resulting in something like this:
UserID StartDate EndDate
====== ========== ==========
1 2011-01-01 2011-02-15 ('A', three rows combined, extending the range)
1 2011-02-20 2011-03-10 ('B', no change, no overlaps here)
2 2011-01-01 2011-01-25 ('C', two rows combined)
Cursors are fine if needed, but if I can do without them that would be even better.
For SQL Server 2005+
Notes:
@t
with your table name