Very new to window functions but think I get the idea.
I'm trying to get a rolling sum of one column in 10 minute intervals (determined from a separate timestamp column)
A working sample might would look like this:
EDIT: fiddle link
Filtered for T2 highlights it better:
There's plenty about the topic like this or this and so on but I'm having trouble finding a solution specific to my requirements.
If I didn't need it by 10 minute intervals it'd be very simple, by 10 rows (current + 9 preceding);
SELECT ID, TIME, P/L,
SUM(P/L) OVER (PARTITION BY ID
ORDER BY TIME
ROWS BETWEEN 9 PRECEDING AND CURRENT ROW) as ROLLING
FROM TABLE
Maybe adding the 10 min constraint is not possible with the window function?
############## EDIT TWO ##############
Ok, so I've figure out the issue and managed to reproduce it in a fiddle
In the real table there are valid duplicate transactions (same ID and TIME), I thought I could pull back microseconds and solve this but unfortunately these aren't stored.
We end up with a weird backwards sum for the dupe's which may not be a huge issue but would rather fix if possible:
########################################
ROWS BETWEEN 9 PRECEDING AND CURRENT ROW
but somethins likeRANGE BETWEEN INTERVAL 10 MINUTE PRECEDING AND CURRENT ROW
?