Guest User

Untitled

a guest
Jun 22nd, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.43 KB | None | 0 0
  1. -- my table is called 'document', is has a type (text) and a document (jsonb) colum
  2.  
  3.  
  4. select d1.*,
  5. (
  6. select to_json(t) from
  7. (select * from document d2
  8. where type = 'author'
  9. and d2.id = (d1.body->>'author_id')::int
  10. ) t
  11. ) as author,
  12. (
  13. select to_json(array_agg(t)) from
  14. (select * from document d3
  15. where type = 'comment'
  16. and (d3.body->>'article_id')::int = d1.id
  17. ) t
  18. ) as comments
  19.  
  20. from document d1
  21. where type = 'article'
Add Comment
Please, Sign In to add comment