diff --git a/scripts-available/CDB_EqualIntervalBins.sql b/scripts-available/CDB_EqualIntervalBins.sql index eef914e..b63ea19 100644 --- a/scripts-available/CDB_EqualIntervalBins.sql +++ b/scripts-available/CDB_EqualIntervalBins.sql @@ -1,8 +1,8 @@ -- -- Calculate the equal interval bins for a given column -- --- @param in_array A numeric or double precision array of numbers to determine the best --- to determine the bin boundary +-- @param in_array An array of numbers to determine the best +-- bin boundary -- -- @param breaks The number of bins you want to find. -- @@ -10,7 +10,8 @@ -- Returns: upper edges of bins -- -- -CREATE OR REPLACE FUNCTION CDB_EqualIntervalBins ( in_array NUMERIC[], breaks INT ) RETURNS NUMERIC[] as $$ + +CREATE OR REPLACE FUNCTION CDB_EqualIntervalBins ( in_array anyarray, breaks INT ) RETURNS anyarray as $$ WITH stats AS ( SELECT min(e), (max(e)-min(e))/breaks AS del FROM (SELECT unnest(in_array) e) AS p) @@ -20,12 +21,4 @@ SELECT array_agg(bins) FROM stats) q; $$ LANGUAGE SQL IMMUTABLE; -CREATE OR REPLACE FUNCTION CDB_EqualIntervalBins ( in_array double precision[], breaks INT ) RETURNS double precision[] as $$ -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; -$$ LANGUAGE SQL IMMUTABLE; +DROP FUNCTION IF EXISTS CDB_EqualIntervalBins( numeric[], integer);