AVG() OVER ...
All functions > WINDOW FUNCTION > AVG() OVER ...
Returns the average of values in the window frame.
Syntax
AVG(expr) [FILTER (WHERE condition) | WITHIN (WHERE condition)] OVER ([PARTITION BY expr [, ...]] [ORDER BY sort_item [, ...]] [ROWS|RANGE|GROUPS frame])
Notes
- Calculates average over a window of rows
- Useful for moving averages and rolling means
- Returns same type as input expression
- NULL values are ignored in the calculation
See also
Examples
FeatureQL
SELECT
-- Running average (v permuted so means differ from v)
f1 := ZIP(ARRAY(1, 2, 3, 4) AS id, ARRAY(30, 10, 20, 5) AS v).TRANSFORM(
SELECT AVG(v) OVER (ORDER BY id ASC).ROUND(2)
).UNWRAP(),
-- Running average counting only rows where v > 15 (10 and 5 excluded from the frame)
f2 := ZIP(ARRAY(1, 2, 3, 4) AS id, ARRAY(30, 10, 20, 5) AS v).TRANSFORM(
SELECT AVG(v) FILTER (WHERE v > 15) OVER (ORDER BY id ASC).ROUND(2)
).UNWRAP()
;Result
| f1 ARRAY | f2 ARRAY |
|---|---|
| [30.00, 20.00, 20.00, 16.25] | [30.00, 30.00, 25.00, 25.00] |