Мой запрос:Комбинируя использование string_agg и имея в Postgres
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';
Выдает ошибку:
ERROR: column "t1.title" must appear in the GROUP BY clause or be used in an aggregate function
То, что я не понимаю, что string_agg является агрегатной функцией, в соответствии с этой страницы человека:
http://www.postgresql.org/docs/9.4/static/functions-aggregate.html
Мои данные:
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'
Ошибка относится к 't1.title' предложения' HAVING'. Этот 't1.title' не присутствует в предложении GROUP BY, и он не содержится в агрегированной функции. –
Нет, я говорю, что 'string_agg' или любая другая функция агрегата не используется в предложении' HAVING' вашего запроса. –