Created
June 21, 2018 17:11
-
-
Save fabiosussetto/11d64d43aa3f3e217c04de5a8dfa7453 to your computer and use it in GitHub Desktop.
This file contains 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 to_json(a) | |
from ( | |
select d1.*, | |
( | |
select to_json(t) from | |
(select * from document d2 | |
where type = 'author' | |
and d2.id = (d1.body->>'author_id')::int | |
) t | |
) as author, | |
( | |
select to_json(array_agg(t)) from | |
(select d3.*, | |
( | |
select to_json(t) from | |
(select * from document d4 | |
where type = 'author' | |
and d4.id = (d3.body->>'author_id')::int | |
) t | |
) as author | |
from document d3 | |
where type = 'comment' | |
and (d3.body->>'article_id')::int = d1.id | |
) t | |
) as comments | |
from document d1 | |
where type = 'article' | |
) a | |
///// | |
{ | |
"id": 107, | |
"type": "article", | |
"body": { | |
"title": "Test article 1", | |
"author_id": 52 | |
}, | |
"author": { | |
"id": 52, | |
"type": "author", | |
"body": { | |
"name": "John doe 46", | |
"email": "[email protected]" | |
} | |
}, | |
"comments": [{ | |
"id": 15949, | |
"type": "comment", | |
"body": { | |
"text": "Test comment 5843", | |
"author_id": 95, | |
"article_id": 107 | |
}, | |
"author": { | |
"id": 95, | |
"type": "author", | |
"body": { | |
"name": "John doe 89", | |
"email": "[email protected]" | |
} | |
} | |
}, { | |
"id": 26407, | |
"type": "comment", | |
"body": { | |
"text": "Test comment 16301", | |
"author_id": 17, | |
"article_id": 107 | |
}, | |
"author": { | |
"id": 17, | |
"type": "author", | |
"body": { | |
"name": "John doe 11", | |
"email": "[email protected]" | |
} | |
} | |
}, { | |
"id": 49655, | |
"type": "comment", | |
"body": { | |
"text": "Test comment 39549", | |
"author_id": 57, | |
"article_id": 107 | |
}, | |
"author": { | |
"id": 57, | |
"type": "author", | |
"body": { | |
"name": "John doe 51", | |
"email": "[email protected]" | |
} | |
} | |
}, { | |
"id": 60344, | |
"type": "comment", | |
"body": { | |
"text": "Test comment 50238", | |
"author_id": 41, | |
"article_id": 107 | |
}, | |
"author": { | |
"id": 41, | |
"type": "author", | |
"body": { | |
"name": "John doe 35", | |
"email": "[email protected]" | |
} | |
} | |
}, { | |
"id": 63287, | |
"type": "comment", | |
"body": { | |
"text": "Test comment 53181", | |
"author_id": 72, | |
"article_id": 107 | |
}, | |
"author": { | |
"id": 72, | |
"type": "author", | |
"body": { | |
"name": "John doe 66", | |
"email": "[email protected]" | |
} | |
} | |
}, { | |
"id": 72609, | |
"type": "comment", | |
"body": { | |
"text": "Test comment 62503", | |
"author_id": 57, | |
"article_id": 107 | |
}, | |
"author": { | |
"id": 57, | |
"type": "author", | |
"body": { | |
"name": "John doe 51", | |
"email": "[email protected]" | |
} | |
} | |
}, { | |
"id": 91950, | |
"type": "comment", | |
"body": { | |
"text": "Test comment 81844", | |
"author_id": 14, | |
"article_id": 107 | |
}, | |
"author": { | |
"id": 14, | |
"type": "author", | |
"body": { | |
"name": "John doe 8", | |
"email": "[email protected]" | |
} | |
} | |
}, { | |
"id": 98428, | |
"type": "comment", | |
"body": { | |
"text": "Test comment 88322", | |
"author_id": 10, | |
"article_id": 107 | |
}, | |
"author": { | |
"id": 10, | |
"type": "author", | |
"body": { | |
"name": "John doe 4", | |
"email": "[email protected]" | |
} | |
} | |
}, { | |
"id": 102108, | |
"type": "comment", | |
"body": { | |
"text": "Test comment 92002", | |
"author_id": 92, | |
"article_id": 107 | |
}, | |
"author": { | |
"id": 92, | |
"type": "author", | |
"body": { | |
"name": "John doe 86", | |
"email": "[email protected]" | |
} | |
} | |
}] | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment