我有一个数组列类型的表:
title tags
"ridealong";"{comedy,other}"
"ridealong";"{comedy,tragedy}"
"freddyjason";"{horror,silliness}"
我想编写一个查询, for each 标题生成一个数组(在理想情况下,它将是一个集/消除重复的数组)
例如
select array_cat(tags),title from my_test group by title
当然,上面的查询不起作用,但我想生成两行:
"ridealong";"{comedy,other,tragedy}"
"freddyjason";"{horror,silliness}"
任何帮助或指点都将不胜感激
基于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;