MEDIAN() OVER ...

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

Returns the median of values in the window frame as DOUBLE.

Syntax

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

Notes

  • Same value as QUANTILE_CONT(x, 0.5) over the window frame; NULL values are ignored
  • Snowflake and BigQuery accept PARTITION BY only: ORDER BY or a frame raises UE/WINDOW-FRAME-NOT-SUPPORTED-DIALECT
  • Not available on Postgres, which has no percentile window function

Availability

StatusBackends
Not availablepostgres, mysql

See also

Examples

FeatureQL
-- Running median to the current row
-- Running median 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).TRANSFORM(
        RETURN MEDIAN(v::DOUBLE) OVER (ORDER BY id ASC).ROUND(2)
    ).UNWRAP(),
    f2 := ZIP(ARRAY(1, 2, 3, 4) AS id, ARRAY(30, 10, 20, 5) AS v).TRANSFORM(
        RETURN MEDIAN(v::DOUBLE) FILTER (WHERE v > 15) OVER (ORDER BY id ASC).ROUND(2)
    ).UNWRAP()
;
Result
f1 ARRAY<DOUBLE>f2 ARRAY<DOUBLE>
[30.00, 20.00, 20.00, 15.00][30.00, 30.00, 25.00, 25.00]