MEDIAN
Definition
There is no MEDIAN function in BigQuery, but it can be calculated using the PERCENTILE_CONT function.
Using this function, you can calculate the median with the following:
PERCENTILE_CONT (value_expression, percentile [{RESPECT | IGNORE} NULLS])By default NULL values are ignored in the calculation but they can be included with the key words RESPECT NULLS. Â If you include NULL values then the following behaviour is important to know.
- Any interpolation between two
NULLvalues returnsNULL. - An interpolation between a
NULLand a non-NULLvalue returns the non-NULLvalue. (NULLis not just simply treated as 0).
In the example below the results are different because we now take the NULL value in the list of numbers into account, shifting the result to 2.
Practical Info
PERCENTILE_CONTis a window function. You can learn more about window functions here.value_expressionmust be eitherNUMERIC,BIGNUMERIC,FLOAT64.percentilemust be a literal between 0 and 1.- If you set
percentileto 0 or 1 you can usepercentile_contto calculate the minimum and maximum values respectively. - The function
PERCENTILE_DISChas the same arguments asPERCENTILE_CONTbut provides a percentile value for a discrete set of values including strings and any data type that can be ordered.
Common Questions
Why is there no MEDIAN function in BigQuery?
There's no official explanation why MEDIAN function isn't supported. BigQuery has a number of approximate aggregate functions which are designed to give approximate results but are much less computationally expensive across big data sets.
Troubleshooting Common Errors
The query could not be executed in the allotted memory. OVER() operator used too much memory
This error occurs when the table you're analysing is too big for BiqQuery to fit in memory. This can happen with big data tables and particularly if the window function has no partition and it is therefore running the calculation across the whole table. Instead you can use the approx_quantiles function to give you an estimated value.
Related Pages
- Window Functions Explained