cartodb-postgresql/scripts-available/CDB_Math.sql

28 lines
582 B
MySQL
Raw Permalink Normal View History

2014-08-20 00:09:27 +08:00
-- CartoDB Math SQL functions
-- Mode
-- https://wiki.postgresql.org/wiki/Aggregate_Mode
2019-05-31 21:29:28 +08:00
CREATE OR REPLACE FUNCTION @extschema@._CDB_Math_final_mode(anyarray)
2014-08-20 00:09:27 +08:00
RETURNS anyelement AS
$BODY$
SELECT a
FROM unnest($1) a
GROUP BY 1
ORDER BY COUNT(1) DESC, 1
LIMIT 1;
$BODY$
2017-10-24 20:16:56 +08:00
LANGUAGE 'sql' IMMUTABLE PARALLEL SAFE;
2014-08-20 00:09:27 +08:00
2019-05-31 21:29:28 +08:00
DROP AGGREGATE IF EXISTS @extschema@.CDB_Math_Mode(anyelement);
2014-08-20 00:09:27 +08:00
2019-05-31 21:29:28 +08:00
CREATE AGGREGATE @extschema@.CDB_Math_Mode(anyelement) (
2014-08-20 00:09:27 +08:00
SFUNC=array_append,
STYPE=anyarray,
2019-05-31 21:29:28 +08:00
FINALFUNC=@extschema@._CDB_Math_final_mode,
PARALLEL = SAFE,
INITCOND='{}'
2014-08-20 00:09:27 +08:00
);