Created
June 20, 2020 15:19
-
-
Save paslandau/14940ec0fd34dc30b36377886c308ab3 to your computer and use it in GitHub Desktop.
Example: Calculate the MEDIAN in BigQuery
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Source: Calculate the MEDIAN in BigQuery