I have (adjusted for the sake of simplicity) a table containing 2 columns – start and end, both of which are time stamps. These represent phone calls.
I need to produce what boils down to a graph, with time along the X axis, and simultaneous calls along the Y axis.
I can extract the number of concurrent calls at any given time quite easily, of course – with something like this:
select
count(*)
from
calls_table
where
time_started < 1282854000 and time_ended > 1282854000
That just gives me the numbers back for the particular time. What I need to be able to do is include a selection of times in my query (for example every hour on the hour) and pull the count of simultaneous calls for that time stamp? All great ideas gratefully received!
If SQL Server 2005+ you can use a recursive CTE that you then join on. Example follows.