REGR_SLOPE() OVER ...
All functions > WINDOW FUNCTION > REGR_SLOPE() OVER ...
Returns the slope of the linear regression line over the window frame.
Syntax
REGR_SLOPE(expr_y, expr_x) [FILTER (WHERE condition) | WITHIN (WHERE condition)] OVER ([PARTITION BY expr [, ...]] [ORDER BY sort_item [, ...]] [ROWS|RANGE|GROUPS frame])
Notes
- Equivalent to COVAR_POP(y, x) / VAR_POP(x) over the window
- NULL values in either expression are excluded
See also
Examples
FeatureQL
SELECT
-- Slope of y on v over the frame; first row is NaN (undefined slope on one point)
f1 := ZIP(
ARRAY(1, 2, 3, 4) AS id,
ARRAY(30, 10, 20, 5) AS v,
ARRAY(2.0e0, 4.0e0, 6.0e0, 8.0e0) AS y
).TRANSFORM(
SELECT REGR_SLOPE(y, CAST(v AS DOUBLE)) OVER (ORDER BY id ASC).ROUND(2)
).UNWRAP(),
-- Slope counting only rows where v > 15; leading NaNs until two filtered rows exist
f2 := ZIP(
ARRAY(1, 2, 3, 4) AS id,
ARRAY(30, 10, 20, 5) AS v,
ARRAY(2.0e0, 4.0e0, 6.0e0, 8.0e0) AS y
).TRANSFORM(
SELECT REGR_SLOPE(y, CAST(v AS DOUBLE)) FILTER (WHERE v > 15) OVER (
ORDER BY id ASC
).ROUND(
2
)
).UNWRAP()
;Result
| f1 ARRAY | f2 ARRAY |
|---|---|
| [null, -0.10, -0.10, -0.18] | [null, null, -0.40, -0.40] |