Skip to content

Instantly share code, notes, and snippets.

@paslandau
Created June 20, 2020 15:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save paslandau/14940ec0fd34dc30b36377886c308ab3 to your computer and use it in GitHub Desktop.
Save paslandau/14940ec0fd34dc30b36377886c308ab3 to your computer and use it in GitHub Desktop.
Example: Calculate the MEDIAN in BigQuery
# Calculate the MEDIAN in BigQuery; standard-sql; 2020-06-20
# @see http://www.pascallandau.com/bigquery-snippets/calculate-median/
WITH data as (
SELECT
1 as id,
"2020-06-20" as day,
10 as quantity
UNION ALL SELECT 2,"2020-06-20", 15
UNION ALL SELECT 1,"2020-06-21",5
UNION ALL SELECT 2,"2020-06-21",10
UNION ALL SELECT 1,"2020-06-22",9
UNION ALL SELECT 2,"2020-06-22",4
),
median_per_row as (
SELECT
*,
PERCENTILE_CONT(quantity, 0.5) OVER(PARTITION BY id) AS median,
FROM data
)
SELECT
id,
ARRAY_AGG((SELECT as STRUCT day, quantity) ORDER BY quantity) as quantities,
ANY_VALUE(median) as median,
FROM
median_per_row
GROUP BY
id
@paslandau
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment