BigQuery · Window function
BigQuery PERCENTILE_CONT
PERCENTILE_CONT(x, p) OVER (...) returns the exact p-th percentile of x with linear interpolation, such as the median with p = 0.5. In BigQuery it is a window function only: add SELECT DISTINCT for one row per group, or use APPROX_QUANTILES in GROUP BY queries.
PERCENTILE_CONT sorts the values of each window partition and returns the value at percentile p, interpolating linearly between the two nearest values when p falls between rows. The median of 10, 20, 30 and 40 is therefore 25, a value that doesn't occur in the data. NULLs are ignored unless you add RESPECT NULLS.
In BigQuery it exists only as a window function. SELECT PERCENTILE_CONT(x, 0.5) FROM t fails with percentile_cont aggregate function is not supported, PostgreSQL's PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY x) is a syntax error, and there is no MEDIAN function. The OVER clause may contain only PARTITION BY: no ORDER BY and no frame. Because the result repeats on every input row, add SELECT DISTINCT, or aggregate the window's output in an outer query, to get one row per group.
With INT64 or FLOAT64 values the result is FLOAT64. For a NUMERIC result, cast the values to NUMERIC and pass a NUMERIC percentile such as NUMERIC '0.5': a plain 0.5 is a FLOAT64 literal, so PERCENTILE_CONT(CAST(x AS NUMERIC), 0.5) still returns FLOAT64. Each window partition is processed in one place, so OVER () on a large table can run out of memory; APPROX_QUANTILES is the scalable alternative.
Syntax
PERCENTILE_CONT(value_expression, percentile [{RESPECT | IGNORE} NULLS]) OVER ([PARTITION BY partition_expression [, ...]])Arguments and return value
| Argument | Description |
|---|---|
value_expression | The values to take the percentile of: NUMERIC, BIGNUMERIC or FLOAT64. INT64 columns are accepted and produce a FLOAT64 result. |
percentile | A literal or query parameter between 0 and 1: 0.5 for the median, 0.9 for p90. |
RESPECT NULLS | IGNORE NULLS | NULLs are ignored by default. With RESPECT NULLS they count as the smallest values; interpolating between two NULLs gives NULL, between a NULL and a value gives the value. |
PARTITION BY partition_expression | One percentile per group. Optional. ORDER BY and frame clauses are not allowed. |
Returns: FLOAT64 when either argument is FLOAT64 or the values are INT64. NUMERIC when values and percentile are both NUMERIC; BIGNUMERIC when one of them is BIGNUMERIC and the other NUMERIC or BIGNUMERIC.
Examples
Median of a column
The window returns the median on every row; SELECT DISTINCT keeps one.
SELECT DISTINCT
PERCENTILE_CONT(amount, 0.5) OVER () AS median_amount
FROM UNNEST([10, 20, 30, 40]) AS amount;| median_amount |
|---|
| 25.0 |
Median and p90 runtime per pipeline
p90 of orders_daily falls 70% of the way from 360 to 900. Without ROUND, the FLOAT64 interpolation shows up as 738.0000000000001.
WITH runs AS (
SELECT 'orders_daily' AS pipeline, 300 AS runtime_s UNION ALL
SELECT 'orders_daily', 340 UNION ALL
SELECT 'orders_daily', 360 UNION ALL
SELECT 'orders_daily', 900 UNION ALL
SELECT 'users_sync', 60 UNION ALL
SELECT 'users_sync', 75
)
SELECT DISTINCT
pipeline,
PERCENTILE_CONT(runtime_s, 0.5) OVER (PARTITION BY pipeline) AS median_s,
ROUND(PERCENTILE_CONT(runtime_s, 0.9) OVER (PARTITION BY pipeline), 1) AS p90_s
FROM runs
ORDER BY pipeline;| pipeline | median_s | p90_s |
|---|---|---|
| orders_daily | 350.0 | 738.0 |
| users_sync | 67.5 | 73.5 |
IGNORE NULLS vs RESPECT NULLS
Ignoring NULLs, the median of 10, 20, 30 is 20. With RESPECT NULLS the two NULLs sort first and the middle of five values is 10.
SELECT DISTINCT
PERCENTILE_CONT(x, 0.5) OVER () AS median_ignore_nulls,
PERCENTILE_CONT(x, 0.5 RESPECT NULLS) OVER () AS median_respect_nulls
FROM UNNEST([NULL, NULL, 10, 20, 30]) AS x;| median_ignore_nulls | median_respect_nulls |
|---|---|
| 20.0 | 10.0 |
Common mistakes
- Calling it as an aggregate.
SELECT PERCENTILE_CONT(x, 0.5) FROM t GROUP BY gfails withpercentile_cont aggregate function is not supported; addOVER (PARTITION BY g)with SELECT DISTINCT, or use APPROX_QUANTILES. - Porting
PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY x). BigQuery stops with a syntax error; it takes the value and the percentile as arguments:PERCENTILE_CONT(x, 0.5) OVER (). - Adding ORDER BY to the OVER clause. It fails with
Window ORDER BY is not allowed for analytic function percentile_cont. - Forgetting SELECT DISTINCT. Without it you get the same percentile repeated on every input row.
- Passing the percentile as a column. It fails with
Argument 2 to PERCENTILE_CONT must be a literal or query parameter.
Cost and performance
OVER ()makes the whole table one window partition, processed by a single worker; on large tables that can fail with Resources exceeded. Partition the window, filter first, or switch to APPROX_QUANTILES, which aggregates in parallel.
FAQ
How do I calculate the median in BigQuery?
Exactly: SELECT DISTINCT PERCENTILE_CONT(x, 0.5) OVER () FROM t, or OVER (PARTITION BY g) per group. Approximately, in a GROUP BY query: APPROX_QUANTILES(x, 2)[OFFSET(1)]. There is no MEDIAN function.
Why does PERCENTILE_CONT need an OVER clause?
BigQuery implements PERCENTILE_CONT only as a window (analytic) function. Use OVER () for the whole table or OVER (PARTITION BY ...) per group, then SELECT DISTINCT.
What is the difference between PERCENTILE_CONT and PERCENTILE_DISC?
PERCENTILE_CONT interpolates and can return a value that isn't in the data (25 for 10, 20, 30, 40). PERCENTILE_DISC returns an actual value (20) and works on any orderable type.
Should I use PERCENTILE_CONT or APPROX_QUANTILES?
PERCENTILE_CONT for exact, interpolated values on data that fits a window partition. APPROX_QUANTILES for large tables, GROUP BY queries and dashboards, where a close estimate is enough.
Related
Errors you may hit
More window functions
All BigQuery window functions · All functions · BigQuery error messages
