CORR() OVER ...

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

Returns the Pearson correlation coefficient over the window frame.

Syntax

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

Notes

  • Returns a value between -1 (perfect negative) and 1 (perfect positive)
  • NULL values in either expression are excluded

See also

Examples

FeatureQL
SELECT
    -- Pearson correlation over the frame; first row is NaN (undefined CORR 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 CORR(CAST(v AS DOUBLE), y) OVER (ORDER BY id ASC).ROUND(2)
    ).UNWRAP(),
    -- Correlation 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 CORR(CAST(v AS DOUBLE), y) FILTER (WHERE v > 15) OVER (ORDER BY id ASC).ROUND(
            2
        )
    ).UNWRAP()
;
Result
f1 ARRAYf2 ARRAY
[null, -1.00, -0.50, -0.76][null, null, -1.00, -1.00]