I have some phone call data in a mssql 2008 database and would like to split it in to 15 (or X) minute intervals to be used in some Erlang calculations.
call logg:
call start end
1 2011-01-01 12:00:01 2011-01-01 12:16:00
2 2011-01-01 12:14:00 2011-01-01 12:17:30
3 2011-01-01 12:29:30 2011-01-01 12:46:20
Would be shown as
call start end
1 2011-01-01 12:00:01 2011-01-01 12:15:00
1 2011-01-01 12:15:00 2011-01-01 12:16:00
2 2011-01-01 12:14:00 2011-01-01 12:15:00
2 2011-01-01 12:15:00 2011-01-01 12:17:30
3 2011-01-01 12:29:30 2011-01-01 12:30:00
3 2011-01-01 12:30:00 2011-01-01 12:45:00
3 2011-01-01 12:45:00 2011-01-01 12:46:20
Does anyone have any good suggestions on how to do this?
Thanks in advance
Sample table
The query
If creating a view off this query, drop the last [order by] line
Notes
dateadd(mi, 15*(datediff(mi, 0, d.start)/15), 0)
calculates the 15-minute border on which the call startedThere are only 1440 minutes in the day. So you could create and populate with 1440 rows a MINUTES table whose primary key is a four-digit number representing hour-minute in 24-hour format (e.g. 9:13PM would be 2113). Then you could have as many columns in that table as you needed to characterize any minute in the day: what quarter-hour it falls into, whether it is considered off-peak or peak, what its billing rate is under Plan A, and so on. You just keep adding columns as your use cases require. Totally extensible.
In your example, the first column MINUTES.QuarterHour would indicate which quarter-hour the minute fell into. 02:17 is in quarter-hour 6, for example. Once the table is populated, all you need to do then is use the HHMM chunk of your datetime value from your phone-calls table to pull-back the quarter-hour to which that time belongs, using a simple join on HHMMChunk = MINUTES.id. The advantages: queries are much simpler and easier to write and maintain and they are probably not as computationally intensive.
EDIT: the approach is also generic and portable (i.e. not implementation-specific).
Fascinating problem!
Just for kicks, here's a PostgreSQL approach, using generate_sequence() to fill out the interior 15 minute intervals. There's undoubtedly a way to coalesce the first two unions that build the first and last intervals, but that is left as an exercise for the reader.
Richard is right, this query splits the calls into 15 minute intervals:
Try this:
Unfortunatelly I do not have a SQL at hand so I cannot test it. This is the idea however to make it so you will probably manage to adjust it in case it fails somewhere.
I put the aliases and the mistake was using m instead of minute. Can you try it to see if it works. TX. (that happens when not testing is done)
To split it at 15 minutes (00/15/30/45) u can use this: