I want to Aggregate a column over timestamps.
Here an example:
Table contains columns like col1, col2, ..., col_ts (timestamp column).
SELECT
SUM(col1) OVER (ORDER BY col_ts ROWS BETWEEN 2 PRECEDING AND 2 FOLLOWING) SUM1,
SUM(col2) OVER (ORDER BY col_ts ROWS BETWEEN 2 PRECEDING AND 2 FOLLOWING) SUM2
FROM ...
Now i want only the 2 PRECEDING and the 2 FOLLOWING ROWS SUMMED when the difference between the timestamps are <= 5 minutes.
For example, lets look at these timestamp values:
14.09.15 15:44:00
14.09.15 15:50:00
14.09.15 15:51:00
14.09.15 15:52:00
14.09.15 15:53:00
When were are at the row with the timestamp value "14.09.15 15:51:00", i want the SUM OVER the values from 15:50 until 15:53, because the difference between 15:50 and 15:44 is bigger than 5 minutes.
Is there a way to write such a condition in the over clause?
Or is there anyone with a good and performant solution to this?
I think it's too much for sql. You can restrict the number or elements in window, you can restrict somehow(see bellow) the values, but not both at the same time.
Results:
(sorry for not taking the exact example as in your question)
The alternative is to write some PL/SQL(open a cursor and do some processing).
ok, i see the problem here. thanks florin. so what about some preprocessing? i could find a solution, but i am not sure if there is a faster solution:
Result:
what do you think?