Created
April 4, 2020 11:15
-
-
Save nurettin/cf5176957cbf08796316c73c3d593ce4 to your computer and use it in GitHub Desktop.
declarative json postgresql
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 row_to_json(q) from ( | |
select | |
row_to_json(a) account | |
, json_agg(t) trades | |
from pm_account a | |
left join lateral ( | |
select | |
row_to_json(t) trade | |
, json_agg(d) dailies | |
, json_agg(sf) smartorders | |
from pm_trade t | |
left join pm_tradedaily d | |
on d.trade_id = t.id | |
left join lateral ( | |
select | |
row_to_json(pts) smartorder | |
, json_agg(ptf) fills | |
from pm_tradesmartorder pts | |
left join pm_tradefill ptf | |
on ptf.trade_smartorder_id = pts.id | |
where pts.trade_id = t.id | |
group by pts.* | |
) sf on 1 = 1 | |
where t.account_id = a.id | |
group by t.* | |
) t on 1 = 1 | |
group by a.* | |
) q |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
one can convert the following schema into the above sql (tree leaves are left join, tree branches are left join lateral)