added CDB_Math_mode

This commit is contained in:
javi 2014-08-19 18:09:27 +02:00
parent 41a2c7363e
commit 723a08e814
5 changed files with 38 additions and 0 deletions

4
NEWS
View File

@ -1,3 +1,7 @@
0.3.X (2014-XX-XX)
------------------
Added CDB_Math_Mode function
0.3.6 (2014-08-11) 0.3.6 (2014-08-11)
------------------ ------------------
Dummy release to solve some issues with cdb branch/tag Dummy release to solve some issues with cdb branch/tag

View File

@ -0,0 +1,26 @@
-- CartoDB Math SQL functions
-- Mode
-- https://wiki.postgresql.org/wiki/Aggregate_Mode
CREATE OR REPLACE FUNCTION cartodb._CDB_Math_final_mode(anyarray)
RETURNS anyelement AS
$BODY$
SELECT a
FROM unnest($1) a
GROUP BY 1
ORDER BY COUNT(1) DESC, 1
LIMIT 1;
$BODY$
LANGUAGE 'sql' IMMUTABLE;
DROP AGGREGATE IF EXISTS cartodb.CDB_Math_Mode(anyelement);
CREATE AGGREGATE cartodb.CDB_Math_Mode(anyelement) (
SFUNC=array_append,
STYPE=anyarray,
FINALFUNC=_CDB_Math_final_mode,
INITCOND='{}'
);

View File

@ -0,0 +1 @@
../scripts-available/CDB_Math.sql

4
test/CDB_MathTest.sql Normal file
View File

@ -0,0 +1,4 @@
SELECT cdb_math_mode(a) from unnest(ARRAY[1,2,2,3]) a;
SELECT cdb_math_mode(a) from unnest(ARRAY[1,2,3]) a;
SELECT cdb_math_mode(a) from unnest(ARRAY[1]) a;

3
test/CDB_MathTest_expect Normal file
View File

@ -0,0 +1,3 @@
2
1
1