There may be ways to do this to avoid a numerical conversion, but this should do the job:
SELECT key, Sum(to_number(value, '999999999999')) FROM ( SELECT (each(goals)).key, (each(goals)).value FROM public.statistics ) as s Group By key
http://sqlfiddle.com/#!1/eb745/10/0
This is a big smell that Postgres doesn't want to bend in this way, but:
create table test (id int, goals hstore); Insert Into Test(id, goals) Values (30059, '3=>123'); Insert Into Test(id, goals) Values (27333, '3=>200,5=>10'); Create Function hagg() returns hstore As 'Declare ret hstore := ('''' :: hstore); i hstore; c cursor for Select hstore(key, (x.Value::varchar)) From (Select key, Sum((s.value::int)) as Value From (Select (each(goals)).* From Test) as s Group By key) as x; BEGIN Open c; Loop Fetch c into i; Exit When Not FOUND; ret := i || ret; END LOOP; return ret; END' Language 'plpgsql';
I could not get the sql script to accept a body with multiple lines, in real postgres you should be able to quote $$ from this and break it down a bit.
http://sqlfiddle.com/#!1/e2ea7/1/0
source share