quickdraw (6/27/2011)
Oh, that row_number trick is so beautiful it makes me want to cry!
If you'd like to see a similar "Row_Number Trick" on steroids 😉 to solve the problem when the dates and times aren't contiguous and are truly overlapping, check out Itzik's article on the subject. The man's use of simple mathematics is something to behold. Here's the link:
http://www.solidq.com/sqj/Pages/2011-March-Issue/Packing-Intervals.aspx
That site does require a membership to read the full article just as SQLServerCentral does. And, like SQLServerCentral, membership is free and safe and they only need your email address. They don't sell your email address nor give it to "interested parties" unless you allow them to by not unchecking some of the "agreement" boxes.
--Jeff Moden
Change is inevitable... Change for the better is not.