I am trying to wrap several queries into 1 complex query.
Here is the scenario. Given a user_id
of 'xxxxx-xx-xxxxx' I have to query for several data points within several tables. I have most of the queries below wrapped up with some JOIN(s) but would really like to see if its possible to get the last queries where I am getting the users name and email wrapped into an ALIAS
column stored as an object? I am not sure if it's possible but here are the queries broken down..
THE LONG WAY
Query 1:
SELECT * FROM requests WHERE created_by = 'xxxx-xx-xxxxx';
This query will return a row as such:
id | created_by | created_date ...
1141 xxxx-xx-xxxxx 2019-04-09 19:33:40.889+00 ...
I then have to run another query to find all approvers for the returned id Query 2:
SELECT * FROM approvers WHERE request_id = 1141
This could possibly return many rows as such:
request_id | user_id | decision | type | .....
1141 xxxxx-xx-xxxxx approve approver ......
1141 aaaaa-aa-aaaaa approve approver ......
1141 bbbbb-bb-bbbbb under review watcher ......
1141 ccccc-cc-ccccc reject approver .......
and finally to find the users name I have to run queries (for each returned row of Query 2) Query 3,4,5,6:
SELECT * FROM users WHERE id = 'xxxx-xx-xxxxx' LIMIT 1
will return:
id | given_name | family_name | email | .....
xxxx-xx-xxxxx John Doe johndoe@email.com ....
Is it possible to run a subquery and create an ALIAS
column of approver_info
with the associated approvers and users stored as a JSON object as such?
Desired Query / Result:
1 Query:
SELECT * FROM requests
WHERE created_by = 'xxxxx-xx-xxxxx'
SUBQUERY HERE ...
That will produce the following result:
id | created_by | created_date | approver_info ...
1141 xxxx-xx-xxxxx 2019-04-09 19:33:40.889+00 [{"id" : "xxxxx-xx-xxxxx", "given_name" : "John", "family_name" : "Doe", "email":"john@email.com","decision" : "approve", "type" : "approver"},
{"id" : "aaaaa-aa-aaaaa", "given_name" : "Sansa", "family_name" : "Stark", "email":"sansa@.com","decision" : "approve", "type" : "approver"},
{"id" : "bbbbb-bb-bbbbb", "given_name" : "Arya", "family_name" : "Stark", "email":"arya@email.com","decision" : "under review", "type" : "watcher"},
{"id" : "ccccc-cc-ccccc", "given_name" : "Ned", "family_name" : "Stark", "email":"ned@email.com","decision" : "approve", "type" : "approver"}]