cartodb-postgresql/scripts-available/CDB_QuantileBins.sql

19 lines
559 B
MySQL
Raw Normal View History

--
-- Determine the Quantile classifications from a numeric array
--
-- @param in_array A numeric array of numbers to determine the best
-- bins based on the Quantile method.
--
-- @param breaks The number of bins you want to find.
--
--
CREATE OR REPLACE FUNCTION CDB_QuantileBins(in_array numeric[], breaks int)
RETURNS numeric[]
AS $$
SELECT
2018-03-08 23:14:08 +08:00
percentile_disc(SELECT Array(generate_series(1, breaks) / breaks::numeric))
WITHIN GROUP (ORDER BY x ASC) AS p
FROM
unnest(in_array) AS x;
2018-03-08 23:14:08 +08:00
$$ language SQL IMMUTABLE STRICT PARALLEL SAFE;