相关疑难解决方法(0)

在分组/聚合期间连接/合并数组值

我有一个数组列类型的表:

 title       tags
"ridealong";"{comedy,other}"
"ridealong";"{comedy,tragedy}"
"freddyjason";"{horror,silliness}"
Run Code Online (Sandbox Code Playgroud)

我想编写一个查询,每个标题生成一个数组(理想情况下,它将是一个set/deduplicated数组)

例如

select array_cat(tags),title from my_test group by title
Run Code Online (Sandbox Code Playgroud)

上面的查询当然不起作用,但我想生成2行:

"ridealong";"{comedy,other,tragedy}"
"freddyjason";"{horror,silliness}"
Run Code Online (Sandbox Code Playgroud)

任何帮助或指针将非常感谢(我使用的是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;
Run Code Online (Sandbox Code Playgroud)

sql postgresql

23
推荐指数
2
解决办法
1万
查看次数

标签 统计

postgresql ×1

sql ×1