2015-05-08 03:49:35 +08:00
|
|
|
--
|
|
|
|
-- Calculate the equal interval bins for a given column
|
|
|
|
--
|
2016-04-28 07:10:01 +08:00
|
|
|
-- @param in_array An array of numbers to determine the best
|
|
|
|
-- bin boundary
|
2015-05-08 03:49:35 +08:00
|
|
|
--
|
|
|
|
-- @param breaks The number of bins you want to find.
|
|
|
|
--
|
|
|
|
--
|
|
|
|
-- Returns: upper edges of bins
|
|
|
|
--
|
|
|
|
--
|
2016-04-21 07:07:00 +08:00
|
|
|
|
2016-04-28 07:10:01 +08:00
|
|
|
CREATE OR REPLACE FUNCTION CDB_EqualIntervalBins ( in_array anyarray, breaks INT ) RETURNS anyarray as $$
|
2016-04-21 07:07:00 +08:00
|
|
|
WITH stats AS (
|
|
|
|
SELECT min(e), (max(e)-min(e))/breaks AS del
|
|
|
|
FROM (SELECT unnest(in_array) e) AS p)
|
|
|
|
SELECT array_agg(bins)
|
|
|
|
FROM (
|
|
|
|
SELECT min + generate_series(1,breaks)*del AS bins
|
|
|
|
FROM stats) q;
|
2017-10-24 20:16:56 +08:00
|
|
|
$$ LANGUAGE SQL IMMUTABLE PARALLEL SAFE;
|
2016-04-28 07:10:01 +08:00
|
|
|
|
|
|
|
DROP FUNCTION IF EXISTS CDB_EqualIntervalBins( numeric[], integer);
|