How can I compress / aggregate / group a table with events dynamically over time.
I have a table with values and time of occurrence.
Something like this:
value_col time_col
3 | 2011-02-16 22:21:05.250
2 | 2011-02-16 21:21:06.170
15 | 2011-02-16 21:21:05.250
I need to aggregate the values by a given time span (e.g. hourly) starting from the first row (latest event). So in this example I want to end up with two rows for hourly aggregation.
5
15
So if a new value comes in:
value_col time_col
6 | 2011-02-16 23:21:05.247
3 | 2011-02-16 22:21:05.250
2 | 2011-02-16 21:21:06.170
15 | 2011-02-16 21:21:05.250
If I would run that query again I want to end up with:
9
17
It should be easy to change the time span in the query. For example compress over the last 30 seconds, past 6 hours, past 24 hours , etc.. How can I do that in oracle and MS SQL?
Thanks to the previous answers I got the idea on how to fulfill all the requirements.
For each record I calculate the time difference to the latest record in milliseconds (or seconds, depending on resolution). I then modulo the difference with the time span that I am currently interested in (e.g. 3600 sec = 1 h).
Then I add that value to the time_col of the same record and group over that.
Create table:
Solution for SQL:
Solution for Oracle:
If I want to aggregate over the last 2 h I just change 3600 to 7200 seconds.
The result is: