Skip to content

Instantly share code, notes, and snippets.

@xivSolutions
Last active August 29, 2015 14:19
Show Gist options
  • Save xivSolutions/a691e341fe2329c64dc8 to your computer and use it in GitHub Desktop.
Save xivSolutions/a691e341fe2329c64dc8 to your computer and use it in GitHub Desktop.
PG: Aggregate parent/Child records into JSON object
select artists.id as artist_id, (select row_to_json(artist_albums)
from
(
select a.name,
(
select array_to_json(array_agg(albums))
from albums where artist_id = a.id
) as albums
from artists as a where a.id = artists.id
) as artist_albums) as artist from artists where artists.id = 12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment