Yesterday, a co-worker came to me with a puzzle, he wanted to write a query that would group records by intervals of 5, 10, 15 minutes, ideally just passing a parameter to the query to specify the time interval, I sat down and after 5 minutes I came up with the solution:
The trick is highlighted, though it looks like the division and multiplication eliminate each other, what is really happening is an integer division, which, multiplied by the same number, gives you the right intervals:
here's the (minutes) values pattern for an interval of 5
minutedivided by 5multiplied by 5
000
100
...
515
615
...
10210
11210
...
from there you can include more fields in your select criteria, make it prettier, etc
+convert(varchar(2),datepart(mi, DTColumn)/@interval*@interval)
11/12/10 10:10 28
11/12/10 10:15 11
11/12/10 10:20 57
declare @interval int
set @interval = 5
selectdatepart(hh, DateTimeColumn)
,datepart(mi, DateTimeColumn)/@interval*@interval
,count(*)
from thetable
groupbydatepart(hh, DateTimeColumn)
,datepart(mi, DateTimeColumn)/@interval*@interval
The trick is highlighted, though it looks like the division and multiplication eliminate each other, what is really happening is an integer division, which, multiplied by the same number, gives you the right intervals:
here's the (minutes) values pattern for an interval of 5
minutedivided by 5multiplied by 5
000
100
...
515
615
...
10210
11210
...
from there you can include more fields in your select criteria, make it prettier, etc
declare @interval int
set @interval = 5
selectconvert(varchar(8), DTColumn, 1)+' '
+convert(varchar(2),datepart(hh, DTColumn))+':'
+convert(varchar(2),datepart(mi, DTColumn)/@interval*@interval)
,count(*)
from the_table
groupbyconvert(varchar(8), DTColumn, 1)+' '
+convert(varchar(2),datepart(hh, DTColumn))+':'
+convert(varchar(2),datepart(mi, DTColumn)/@interval*@interval)
which produces something like:
11/12/10 10:10 28
11/12/10 10:15 11
11/12/10 10:20 57
hope this is useful