I have a table that contains the following information:
Conf_Start_Time
Part_Start_Time
Part_End_Time
A record is considered to be active at time t if t falls between Part_Start_Time and Part_End_Time.
What I’d like to do is analyze all the records to determine how many records are active given a specified day. My proposed solution is to loop through each minute in the day (say from 6AM to 9PM) and check each record on that day to determine if the user was active at the specified time, t.
Is there a solution to this in SQL, or should I proceed with the code solution?
In code I would pull all the records to memory, loop through time (6AM to 9PM) and test each record on the specified day to determine if it was active at the current time. If it’s active, I would increment a counter, if not, proceed to the next record. Next time around, reinitialize the counter and proceed looping through the day.
We’re using SQL Server 2005.
UPDATE: The output I’m looking for would be an array of maximum concurrent usage from 6AM to 9PM
Record Conf_Start_Time Part_Start_Time Part_End_Time
1. 6/5/2012 13:40:00 6/5/2012 13:41:23 6/5/2012 13:45:27
2. 6/5/2012 13:40:00 6/5/2012 13:40:23 6/5/2012 13:47:29
3. 6/5/2012 13:40:00 6/5/2012 13:42:55 6/5/2012 13:44:17
So at time 13:40:00 0 records are active; at time 13:41:00 1 record is active; at time 13:42:00 2 records are active; at time 13:43:00 3 records are active;
And I need the data for each minute in the day. And then each day in the month. Can this type of looping even be done in SQL?
The following uses correlated subqueries to get the numbers you want. The idea is to count the number of cumulative starts and cumulative ends, up to each time:
The output is based on each time present in the data.
As a rule of thumb, you don’t want to be doing lots of data work on the application side. When possible, that is best done in the database.
This query will have duplicates when there are multiple starts and ends at the same time. In this case, you would need to determine how to treat this case. But, the idea is the same. The outer select would be:
and you need a group by clause:
The “max” gives the starts precedence (meaning with the same time stampt, the starts are treated as happening first, so you get the maximum actives at that time). “Min” would give the ends precedence. And, if you use average, remember to convert to floating point: