COVAR_SAMP() OVER ...

All functions > WINDOW FUNCTION > COVAR_SAMP() OVER ...

Returns the sample covariance over the window frame.

Syntax

COVAR_SAMP(expr_y, expr_x) [FILTER (WHERE condition) | WITHIN (WHERE condition)] OVER ([PARTITION BY expr [, ...]] [ORDER BY sort_item [, ...]] [ROWS|RANGE|GROUPS frame])

Notes

  • Computes sample covariance (divides by N-1) over a window
  • NULL values in either expression are excluded

See also

Examples

FeatureQL
-- Sample covariance over the frame (id is only the sort key)
-- Sample covariance counting only rows where v > 15 (10 and 5 excluded from the frame)
RETURN
    f1 := ZIP(
        ARRAY(1, 2, 3, 4) AS id,
        ARRAY(30, 10, 20, 5) AS v,
        ARRAY(1.0e0, 2.0e0, 4.0e0, 8.0e0) AS x
    ).TRANSFORM(RETURN COVAR_SAMP(v::DOUBLE, x) OVER (ORDER BY id ASC).ROUND(2)).UNWRAP(),
    f2 := ZIP(
        ARRAY(1, 2, 3, 4) AS id,
        ARRAY(30, 10, 20, 5) AS v,
        ARRAY(1.0e0, 2.0e0, 4.0e0, 8.0e0) AS x
    ).TRANSFORM(
        RETURN COVAR_SAMP(v::DOUBLE, x) FILTER (WHERE v > 15) OVER (ORDER BY id ASC).ROUND(
            2
        )
    ).UNWRAP()
;
Result
f1 ARRAY<DOUBLE>f2 ARRAY<DOUBLE>
[null, -10.00, -5.00, -24.58][null, null, -15.00, -15.00]