CUME_DIST() OVER ...

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

Returns the cumulative distribution: (number of rows <= current row) / (total rows).

Syntax

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

Notes

  • Returns a value between 0 (exclusive) and 1 (inclusive)
  • Requires ORDER BY clause to determine row ordering
  • PARTITION BY creates independent distribution groups

See also

Examples

FeatureQL
SELECT
    -- Cumulative fraction of rows at or before current value
    f1 := ZIP(ARRAY(1, 2, 3) AS id, ARRAY(10, 20, 30) AS v).TRANSFORM(
        SELECT CUME_DIST() OVER (ORDER BY v ASC).ROUND(2)
    ).UNWRAP(),
    -- Cumulative distribution over matching rows only; rows outside WITHIN return NULL
    f2 := ZIP(
        ARRAY(1, 2, 3, 4) AS id,
        ARRAY(20, 10, 20, 40) AS s,
        ARRAY(TRUE, FALSE, TRUE, TRUE) AS keep
    ).TRANSFORM(
        SELECT CUME_DIST() WITHIN (WHERE keep) OVER (ORDER BY s ASC).ROUND(2)
    ).UNWRAP()
;
Result
f1 ARRAYf2 ARRAY
[0.33, 0.67, 1.00][0.67, null, 0.67, 1.00]