opts for array version of percentile_disc

This commit is contained in:
Andy Eschbacher 2018-03-08 10:00:52 -05:00
parent dbaf795a79
commit 0467c075f7

View File

@ -10,10 +10,9 @@
CREATE OR REPLACE FUNCTION CDB_QuantileBins(in_array numeric[], breaks int)
RETURNS numeric[]
AS $$
SELECT array_agg(p) FROM (
SELECT percentile_disc(idx::numeric / breaks::numeric)
SELECT
percentile_disc(Array(generate_series(1, breaks) / breaks::numeric))
WITHIN GROUP (ORDER BY x ASC) AS p
FROM generate_series(1, breaks) AS idx, unnest(in_array) AS x
GROUP BY idx
) AS quantiles;
FROM
unnest(in_array) AS x;
$$ language sql;