SELECT `justfunctions.eu.percentiles`([1.2, 2.3, 3.2, 4.2, 5], 50)
/*--Output--
3.2
*/
CREATE OR REPLACE FUNCTION `your_project_id.your_dataset_id.percentiles`(`arr` array<float64>, `percentile` int)
RETURNS float64 AS ((
SELECT
COALESCE(arr[SAFE_OFFSET(CAST(ARRAY_LENGTH(arr)*percentile/100 AS INT)-1)],COALESCE(arr[SAFE_OFFSET (0)],999))
FROM (SELECT ARRAY_AGG(x IGNORE NULLS ORDER BY x) AS arr FROM UNNEST(arr) AS x)
)
)
OPTIONS ( description = '''Finds the specified <percentile> of an array <arr>.''')
SQL User Defined Function (SQL UDF)
See something wrong? Contact us or report an issue on Github.