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
/*--Output--
20
*/
CREATE OR REPLACE AGGREGATE FUNCTION `your_project_id.your_dataset_id.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.''')
SQL User Defined Aggregate Function (SQL UDAF)
See something wrong? Contact us or report an issue on Github.