Reputation: 3427
If I have a data frame with only number of clicks at certain fixed time interval looking like this:
1
3
4
2
6
1
And I want to calculate their rolling average with the 5 rows above, would this be legit:
SELECT AVG(value) OVER (ORDER BY 1 ASC ROWS 4 PRECEDING ) AS avg_value FROM df GROUP BY 1
Or should it be
SELECT AVG(value) OVER (PARTITION BY 1 ASC ROWS 4 PRECEDING) AS avg_value FROM df GROUP BY 1
Upvotes: 0
Views: 905
Reputation: 1269763
You seem to want:
SELECT df.*,
AVG(value) OVER (ORDER BY datetimecol ASC
ROWS 4 PRECEDING
) AS avg_rolling_5
FROM df;
Notes:
datetimecol
is for the column that represents that ordering.order by
is needed.Upvotes: 0