Sql 在postgres中结合使用string_agg和have
我的问题是:Sql 在postgres中结合使用string_agg和have,sql,postgresql,group-by,having,Sql,Postgresql,Group By,Having,我的问题是: select s.id, s.title, string_agg(t1.title, ',') as a, string_agg(t2.title, ',') as b, string_agg(t3.title, ',') as c, string_agg(t4.title, ',') as d from show as s left join tag as t1 on t1.show_id = s.id and t1.typ
select
s.id,
s.title,
string_agg(t1.title, ',') as a,
string_agg(t2.title, ',') as b,
string_agg(t3.title, ',') as c,
string_agg(t4.title, ',') as d
from show as s
left join tag as t1 on t1.show_id = s.id and t1.type='a'
left join tag as t2 on t2.show_id = s.id and t2.type='b'
left join tag as t3 on t3.show_id = s.id and t3.type='c'
left join tag as t4 on t4.show_id = s.id and t4.type='d'
group by
s.id,
s.title
having
t1.title = 'Something';
抛出错误:
错误:列“t1.title”必须出现在GROUP BY子句中或
用于聚合函数中
我不明白的是,根据本手册页,string_agg是聚合函数:
我的数据:
show:
id: 1
title: 'test'
tag:
id: 1
show_id: 1
type: 'a'
title: 'title a'
tag:
id: 2
show_id: 1
type: 'b'
title: 'title a'
tag:
id: 3
show_id: 1
type: 'c'
title: 'title c'
tag:
id: 4
show_id: 1
type: 'd'
title: 'title d'
having命令正在尝试搜索聚合字段 或者按s.title搜索。或按t1.title分组
有
s、 标题=‘某物’;
或者可以更改必须在concat字段上搜索
拥有
字符串_agg(t1.title,',')='Something' 错误是指
t1.HAVING
子句的标题。中的t1.title
不存在于GROUP BY
子句中,也不包含在聚合函数中。不,我是说在查询的HAVING
子句中不使用string_agg
或任何其他聚合函数。