Aggregate hstore column in PostreSQL

跟風遠走 提交于 2019-11-29 14:37:14

问题


I have a table like this:

                         Table "public.statistics"

id         | integer                | not null default nextval('statistics_id_seq'::regclass)
goals      | hstore                 | 

items:

|id    |goals                  |
|30059 |"3"=>"123"             |
|27333 |"3"=>"200", "5"=>"10"  |

What I need to do for aggregate all values by key in hash?

I want to get result like this:

select sum(goals) from statistics

return

|goals                 |
|"3"=>"323", "5"=>"10" |

回答1:


Building on Laurence's answer, here's a pure SQL way to aggregate the summed key/value pairs into a new hstore using array_agg and the hstore(text[], text[]) constructor.

http://sqlfiddle.com/#!1/9f1fb/17

SELECT hstore(array_agg(hs_key), array_agg(hs_value::text))
FROM (
  SELECT
    s.hs_key, sum(s.hs_value::integer)
  FROM (
    SELECT (each(goals)).* FROM statistics
  ) as s(hs_key, hs_value)
  GROUP BY hs_key
) x(hs_key,hs_value)

I've also replaced to_number with a simple cast to integer and simplified the key/value iteration.




回答2:


There might be ways to do this to avoid the numeric conversion, but this should get the job done:

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 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 couldn't get sql fiddle to accept a multi line function body, in real postgres, you should be able to $$ quote this and break it up a bit.

http://sqlfiddle.com/#!1/e2ea7/1/0



来源:https://stackoverflow.com/questions/13020251/aggregate-hstore-column-in-postresql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!