问题描述
自定义汇总
CREATE TABLE my_test(title text, tags text[]);
INSERT INTO my_test(title, tags) VALUES
('ridealong', '{comedy,other}'),
('ridealong', '{comedy,tragedy}'),
('freddyjason', '{horror,silliness}');
CREATE AGGREGATE array_cat_agg(anyarray) (
SFUNC=array_cat,
STYPE=anyarray
);
select title, array_cat_agg(tags) from my_test group by title;
横向查询
…或者由于您不想保留订单并希望进行重复数据删除,因此可以使用LAteraL
类似以下的查询:
SELECT title, array_agg(disTINCT tag ORDER BY tag)
FROM my_test, unnest(tags) tag
GROUP BY title;
在这种情况下,您不需要自定义聚合。由于重复数据删除,对于大数据集而言,这可能要慢一些。不过,ORDER BY
如果不需要的话,删除可能会有所帮助。
解决方法
我有一个表的数组列类型:
title tags
"ridealong";"{comedy,other}"
"ridealong";"{comedy,tragedy}"
"freddyjason";"{horror,silliness}"
我想写一个查询,每个标题生成一个数组(理想情况下,它将是一个设置/去重复的数组)
例如
select array_cat(tags),title from my_test group by title
上面的查询当然行不通,但是我想产生2行:
"ridealong";"{comedy,other,silliness}"
任何帮助或指针将不胜感激(我使用的是Postgres 9.1)
基于Craig的帮助,我得出以下结论(语法略有更改,因为9.1完全按照他的显示抱怨该查询)
SELECT t1.title,array_agg(DISTINCT tag.tag)
FROM my_test t1,(select unnest(tags) as tag,title from my_test) as tag
where tag.title=t1.title
GROUP BY t1.title;