Quantiles function in BigQuery Standard SQL
Asked Answered
C

2

5

BigQuery with Legacy SQL has a pretty convenient QUANTILES function to quickly get a histogram of values in a table without specifying the buckets by hand.

I can't find a nice equivalent in aggregation functions available in Standard SQL. Did I miss something obvious, or otherwise, what's the standard way of emulating it?

Colchicum answered 10/5, 2017 at 13:43 Comment(0)
L
17

You're looking for the APPROX_QUANTILES function :) One of the examples from the docs is:

#standardSQL
SELECT APPROX_QUANTILES(x, 2) AS approx_quantiles
FROM UNNEST([NULL, NULL, 1, 1, 1, 4, 5, 6, 7, 8, 9, 10]) AS x;

+------------------+
| approx_quantiles |
+------------------+
| [1, 5, 10]       |
+------------------+

Note that it returns an array, but if you want the elements of the array as individual rows, you can unnest the result:

#standardSQL
SELECT
  quant, offset
FROM UNNEST((
  SELECT APPROX_QUANTILES(x, 2) AS quants
  FROM UNNEST([NULL, NULL, 1, 1, 1, 4, 5, 6, 7, 8, 9, 10]) AS x
)) AS quant WITH OFFSET
ORDER BY offset;
Loquat answered 10/5, 2017 at 14:43 Comment(0)
T
0

If approximate values don't work for you, you can use the PERCENTILE_CONT function.

Tuna answered 12/1 at 19:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.