I’ve got a table that has a series of start dates and end dates, and I’m trying to figure out how many unique sessions exist–in other words, how many distinct time periods that don’t overlap.
So if the table looks like this:
ID | StartTime | EndTime
A | 1/1/11 12:00 | 1/1/11 14:00
B | 1/1/11 12:30 | 1/1/11 15:00
C | 1/1/11 14:30 | 1/1/11 15:30
D | 1/1/11 16:00 | 1/1/11 16:30
E | 1/1/11 16:30 | 1/1/11 17:00
what kind of query would return a count of 3 sessions since records A, B and C overlap?
Try this. Returns correct count in SQL Server 2008.