I have a continous time dataset and I want to break it up into 15 minute blocks using sql.
I don’t want to have to create a new table to be able to do this if I can help it.
i.e.
Time, Count
09:15, 1
09:30, 3
09:45, 0
10:00, 2
10:15, 3
…..
Does anyone have any idea of how I can do this. I presume that is use a select similar to the following:
SELECT [Some kind of data manipulation on “MyDate”]
, COUNT(ID)
FROM MyTable
GROUP BY [Some kind of data manipulation on “MyDate”]
With careful use of dateadd and datediff, this can be accomplished. My solution rounds the times down.
The first piece calculates the number of minutes between the row’s date and the epoch (0) and does a mod 15 on it, giving the difference between the row’s date and the closest 15 minute interval:
Next, we need to deal with just the minutes, so we use this date-part stripping technique I learned from SQL Server Magazine February 2007 (Datetime Calculations by Itzik Ben-Gan):
Then, we add the difference to the row’s date column and group and count and voila!