I have two tables: a schedule table that contains information about how an employee is scheduled and a numbers table in which each number corresponds to a date.
The tables look like:
[Employee Schedule]
ID Employee ID Project ID Day ID
----------- ----------- ----------- -----------
1 64 2 168
2 64 2 169
3 64 2 170
4 64 2 171
5 64 1 169
6 64 1 170
7 64 1 171
8 64 1 172
9 64 2 182
10 64 2 183
11 64 2 184
and
[Day Numbers]
ID Day
----------- ----------
168 2009-06-18
169 2009-06-19
170 2009-06-20
171 2009-06-21
172 2009-06-22
173 2009-06-23
174 2009-06-24
175 2009-06-25
176 2009-06-26
177 2009-06-27
178 2009-06-28
179 2009-06-29
180 2009-06-30
181 2009-07-01
182 2009-07-02
183 2009-07-03
184 2009-07-04
As you can see, Employee 64 is scheduled on project 1 from 2009-06-19 to 2009-06-22 and project 2 from 2009-06-18 to 2009-06-21 and again from 2009-07-02 to 2009-07-04.
My question is: what algorithm can I use to quickly determine the spans of the employee's schedule in a fashion such that I can display it as follows?
Employee ID Project ID Duration
----------- ---------- ------------
64 1 2009-06-19 to 2009-06-22
64 2 2009-06-18 to 2009-06-21
64 2 2009-07-02 to 2009-07-04
I can do this on the SQL side or the code side. I have Linq at my disposal if I need it. The table doesn't need to be compiled by SQL. This will happen dynamically on a website and should be as efficient as possible. I don't want to have to iterate through each and look for breaks in contiguous days if I don't have to.