This is a follow-up on my previous post How calculate the fraction of rows fulfilling a criterion
I have a time-stamped table of a kind:
timestamp: timestamp
field1: double
...
more fields
What I would like to achieve is a time stamped, rolling fraction of rows fulfilling a criterion in a certain time window. For example, the fraction of rows fulfilling "field1 > crit" in the last minute/5 hours/day.
I already found a way to calculate the average of field1 over a sliding window of rows:
SELECT timestamp, avg(a.field1)
OVER(ORDER BY a.timestamp ROWS BETWEEN 29 PRECEDING AND CURRENT ROW)
AS output_name
FROM schema."table" a;
But I do not manage to extend this to what I need.
You're looking for the
rangewindow frame clause: demo