For my problem, we have a schema whereby one photo has many tags and also many comments. So if I have a query where I want all the comments and tags, it will multiply the rows together. So if one photo has 2 tags and 13 comments, I get 26 rows for that one photo:
SELECT
tag.name,
comment.comment_id
FROM
photo
LEFT OUTER JOIN comment ON comment.photo_id = photo.photo_id
LEFT OUTER JOIN photo_tag ON photo_tag.photo_id = photo.photo_id
LEFT OUTER JOIN tag ON photo_tag.tag_id = tag.tag_id
That's fine for most things, but it means that if I GROUP BY
and then json_agg(tag.*)
, I get 13 copies of the first tag, and 13 copies of the second tag.
SELECT json_agg(tag.name) as tags
FROM
photo
LEFT OUTER JOIN comment ON comment.photo_id = photo.photo_id
LEFT OUTER JOIN photo_tag ON photo_tag.photo_id = photo.photo_id
LEFT OUTER JOIN tag ON photo_tag.tag_id = tag.tag_id
GROUP BY photo.photo_id
Instead I want an array that is only 'suburban' and 'city', like this:
[
{"tag_id":1,"name":"suburban"},
{"tag_id":2,"name":"city"}
]
I could json_agg(DISTINCT tag.name)
, but this will only make an array of tag names, when I want the entire row as json. I would like to json_agg(DISTINCT ON(tag.name) tag.*)
, but that's not valid SQL apparently.
How then can I simulate DISTINCT ON
inside an aggregate function in Postgres?
question from:
https://stackoverflow.com/questions/30077639/distinct-on-in-an-aggregate-function-in-postgres 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…