This is a follow up to: TSQL Group by N Seconds . (I got what I asked for, but didn’t ask for the right thing)
How can I get a rolling average of 1 second groups of count(*)?
So I want to return per second counts, but I also want to be able to smooth that out over certain intervals, say 10 seconds.
So one method might be to take the average per second of every 10 seconds, can that be done in TSQL?
Ideally, the time field would be returned in Unix Time.
SQL Serveris not particularly good in rolling/cumulative queries.You can use this:
, however, this may not be very efficient, since it will count the same overlapping intervals over an over again.
For the larger invervals, it may be even better to use a
CURSOR-based solution that would allow to keep intermediate results (though normally they are worse performance-wise than pure set-based solutions).OracleandPostgreSQLsupport this clause:which keeps an internal window buffer and is very efficient.
SQL Server, unfortunately, does not support moving windows.