APPROX_PERCENTILE() OVER ...

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

Returns the approximate value at the given percentile in the window frame.

Syntax

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

Notes

  • Uses approximate algorithms for efficient computation on large datasets
  • Percentile must be between 0.0 and 1.0 (e.g. 0.5 for median)
  • FeatureQL accepts exactly two arguments; extra backend-only arguments are rejected
  • NULL values are excluded from the calculation
  • Returns the same type as the value expression

See also

Examples

FeatureQL
-- Running approximate median (0.5 percentile) to the current row
-- Approximate median counting only rows where v >= 20
RETURN
    f1 := ZIP(ARRAY(1, 2, 3, 4, 5) AS id, ARRAY(10, 20, 30, 40, 50) AS v).TRANSFORM(
        RETURN APPROX_PERCENTILE(v, 0.5) OVER (ORDER BY id ASC)
    ).UNWRAP(),
    f2 := ZIP(ARRAY(1, 2, 3, 4, 5) AS id, ARRAY(10, 20, 30, 40, 50) AS v).TRANSFORM(
        RETURN APPROX_PERCENTILE(v, 0.5) FILTER (WHERE v >= 20) OVER (ORDER BY id ASC)
    ).UNWRAP()
;
Result
f1 ARRAY<BIGINT>f2 ARRAY<BIGINT>
[10, 15, 20, 25, 30][null, 20, 25, 30, 35]