Two temp tables are created and then loaded…Here’s the schema.
Create table #SH ([date] datetime,
sched_id int,
agent_id int)
Create table #SD (sched_id int,
start_minute int,
length int,
exception_code int)
(Schema and design is something I can’t change unfortunately, both temp tables are loaded from flat files. I can introduce and load new temp tables if needed).
A little background – The #SH header table holds a persons schedule as ‘Start_minute’ and goes for ‘schedule_length’ in minutes. For example, if start minute and schedule length were both 480, that would read as 8am (8am = 480th minute) and goes until 4pm (480 minutes later, 4pm = 960th minute)
The #SD table holds exceptions to the header. In the example above, the person would likely have a lunch exception which would be start_minute = 720 and length of 30 (12:00 – 12:30).
Date and agent_id is the only thing I’m interested out of #SH, the exception info in #sd is what I’m interested in.
This query works:
Select [date],#sd.start_minute,#sd.length,#sd.start_minute + #sd.length as 'end_minute',agent_id
from #SH
inner join #SD on #SD.sched_id = #sh.sched_id
*end_minute is ultimately a calculated value of start+length = end
This returns something like:
Date Start length end
1 2010-11-11 600 30 630
2 2010-11-11 630 40 670
3 2010-11-11 750 15 765
4 2010-11-11 800 40 840
Now I wish I could say this was over and walk away…but data entry issues exist. In line 1 and 2, the end time of line 1 lines up with the start time in line 2 and should be combined so my results look like this :
Date Start length end
1 2010-11-11 600 70 670
2 2010-11-11 750 15 765
3 2010-11-11 800 40 840
Any idea’s on how to build this logic so I get the 3 lines back instead of 4? I’m working on joining the table to itself on #sd1.start + #sd1.length = #sd2.start for now.
And to further complicate…the example above was 2 lines that needed combined. I’ve come across a record that had 30 1 minute entries in succession that I will need to make into a single record. Fortunately they cannot overlap (you won’t have 2 records occupying the same minutes), but I don’t think the join statement I’m considering above will work for that.
No need for a CTE, all you need is a helper table. Create it once, like so:
Then, all you need is a bit of tricksiness:
Remember, a lot of SQL problems can be solved by rephrasing them. Don’t ask “which ranges do not have a gap”, ask “which minutes have an interval”. The rest follows from there.