median_approx(col)
Finds the median of the specified column <col> by calculating approximate quantiles (1% granularity approximation). This approach provides an efficient and fast estimate of the median value.
CREATE OR REPLACE AGGREGATE FUNCTION `justfunctions.eu.median_approx`(`col` float64)
RETURNS float64 AS ((APPROX_QUANTILES(col, 100))[OFFSET(50)])
OPTIONS ( description = '''Finds the median of the specified column <col> by calculating approximate quantiles (1% granularity approximation). This approach provides an efficient and fast estimate of the median value.''')
Example
WITH sample AS (
SELECT d FROM UNNEST(ARRAY[1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31,33,35,37,39]) AS d
)
SELECT `justfunctions.eu.median_approx`(d)
FROM sample
20