BigQuery - calculating median

BigQuery - calculating median

You can use PERCENTILE_CONT and PERCENTILE_DISC to calculate ~median. However, it doesn’t work as aggregate function in GROUP BY.

APPROX_QUANTILES comes handy here using APPROX_QUANTILES(column, 100)[OFFSET(50)]

SELECT APPROX_QUANTILES(x, 100)[OFFSET(50)] AS median
FROM UNNEST([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) AS x;

22. Apr 2023