Grouping records based on intervals of time
select dateadd(minute, 1+datediff(minute, 0, CaptureTime), 0),
sum(SnapShotValue)
from YourTable
group by dateadd(minute, 1+datediff(minute, 0, CaptureTime), 0)
SE-Data
datediff(minute, 0, CaptureTime)
gives you the number of minutes since 1900-01-01T00:00:00
.
dateadd(minute, 1+datediff(minute, 0, CaptureTime), 0)
adds the number of minutes since 1900-01-01T00:00:00
to 1900-01-01T00:00:00
ending up with a datetime with only minutes.
The 1+
is there because you wanted the next minute.
To do the same with a 5 minute interval you need to do some calculations. Divide the minutes with 5
and multiply with 5
gives you the minutes rounded down to a 5 minute precision. This works because the result of an integer division in SQL Server is an integer.
dateadd(minute, 5 + (datediff(minute, 0, CaptureTime) / 5) * 5, 0)