Last active
August 29, 2015 14:19
-
-
Save xivSolutions/a691e341fe2329c64dc8 to your computer and use it in GitHub Desktop.
PG: Aggregate parent/Child records into JSON object
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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