在GROUP BY中聚合Postgres中的hstore

时间:2017-03-21 21:04:18

标签: sql postgresql postgresql-9.5

我有像这样的hstore数据:

|brand|account|likes|views                 | 
|-----|-------|-----|----------------------|
|Ford |ford_uk|1    |"3"=>"100"            |
|Ford |ford_us|2    |"3"=>"200", "5"=>"10" |
|Jeep |jeep_uk|3    |"3"=>"300"            |
|Jeep |jeep_us|4    |"3"=>"400", "5"=>"20" |

我希望能够按键分类hstores,按品牌分组:

|brand|likes|views                 | 
|-----|-----|----------------------|
|Ford |3    |"3"=>"300", "5"=>"10" |
|Jeep |7    |"3"=>"700", "5"=>"20" |

This answer为没有GROUP BY的方法提供了一个很好的解决方案。使其适应这种情况会产生类似的结果:

SELECT
  sum(likes) AS total_likes,
 (SELECT hstore(array_agg(key), array_agg(value::text))
  FROM (
    SELECT s.key, sum(s.value::integer)
    FROM (
      SELECT((each(views)).*)
    ) AS s(key, value)
    GROUP BY key
  ) x(key, value)) AS total_views
FROM my_table
GROUP BY brand

然而,这给出了:

  

错误:子查询使用未分组的列" my_table.views"来自外部查询

任何帮助表示赞赏!

2 个答案:

答案 0 :(得分:4)

这是因为在views查询中使用了group by列没有聚合函数 非常快速的解决方法:

with my_table(brand,account,likes,views) as (
  values
    ('Ford', 'ford_uk', 1, '"3"=>"100"'::hstore),
    ('Ford', 'ford_uk', 2, '"3"=>"200", "5"=>"10"'),
    ('Jeep', 'jeep_uk', 3, '"3"=>"300"'::hstore),
    ('Jeep', 'jeep_uk', 4, '"3"=>"400", "5"=>"20"'))
SELECT
  brand,
  sum(likes) AS total_likes,
 (SELECT hstore(array_agg(key), array_agg(value::text))
  FROM (
    SELECT s.key, sum(s.value::integer)
    FROM 
      unnest(array_agg(views)) AS h, --<< aggregate views according to the group by, then unnest it into the table
      each(h) as s(key,value)
    GROUP BY key
  ) x(key, value)) AS total_views
FROM my_table
GROUP BY brand

<强>更新

您也可以为此类任务创建aggregate

--drop aggregate if exists hstore_sum(hstore);
--drop function if exists hstore_sum_ffunc(hstore[]);
create function hstore_sum_ffunc(hstore[]) returns hstore language sql immutable as $$
  select hstore(array_agg(key), array_agg(value::text))
  from
    (select s.key, sum(s.value::numeric) as value
     from unnest($1) as h, each(h) as s(key, value) group by s.key) as t
$$;
create aggregate hstore_sum(hstore) 
(
    SFUNC = array_append,
    STYPE = hstore[],
    FINALFUNC = hstore_sum_ffunc,
    INITCOND = '{}'
);

之后,您的查询将更简单,更符合规范&#34;:

select
  brand, 
  sum(likes) as total_likes,
  hstore_sum(views) as total_views
from my_table
group by brand;

更新2

即使没有create aggregate,函数hstore_sum_ffunc也可能有用:

select
  brand, 
  sum(likes) as total_likes,
  hstore_sum_ffunc(array_agg(views)) as total_views
from my_table
group by brand;

答案 1 :(得分:1)

如果您为hstore创建聚合,则会更容易:

create aggregate hstore_agg(hstore) 
(
  sfunc = hs_concat(hstore, hstore),
  stype = hstore
);

然后你可以这样做:

with totals as (
  select t1.brand,
         hstore(k, sum(v::int)::text) as views
  from my_table t1, each(views) x(k,v)
  group by brand, k
) 
select brand, 
       (select sum(likes) from my_table t2 where t1.brand = t2.brand) as likes, 
       hstore_agg(views) as views
from totals t1
group by brand;

另一种选择是将可能缓慢的共同相关子查询移动到CTE中:

with vals as (
  select t1.brand,
         hstore(k, sum(v::int)::text) as views
  from my_table t1, each(views) x(k,v)
  group by brand, k
), view_totals as (
  select brand, 
         hstore_agg(views) as views
  from vals
  group by brand
), like_totals as (
  select brand, 
         sum(likes) as likes
  from my_table
  group by brand
)
select vt.brand, 
       lt.likes,
       vt.views
from view_totals vt
  join like_totals lt on vt.brand = lt.brand
order by brand;