clear markdown compare help best fiddles feedback dbanow.uk
clear markdown donate comments/suggestions/bugs a leap of faith? bible365 diddy dollings
By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0. 2805438 fiddles created (40859 in the last week).

create or replace function f_sum_distinct (numeric[], numeric) returns numeric[] language sql as $$ select $1||$2; $$;
 hidden batch(es)


create or replace function f_sum_distinct_final (numeric[]) returns numeric language sql as $$ select sum(v) from (select distinct unnest($1) v) z; $$;
 hidden batch(es)


create aggregate sum_distinct(numeric) ( sfunc = f_sum_distinct ,stype = numeric[] ,finalfunc = f_sum_distinct_final );
 hidden batch(es)


with w(v) as (select 2 union all select 2 union all select 3) select sum(v) "Plain SUM", sum(distinct v) "SUM(DISTINCT)", sum_distinct(v) "SUM_DISTINCT" from w;
Plain SUM SUM(DISTINCT) SUM_DISTINCT
7 5 5
 hidden batch(es)