I have the below tables:
team
col_name type id string name string coach_id string player
col_name type id string name string team_id string main_skill_id string coach
col_name type id string name string skill
col_name type id string name string
I'm using the below query in PostgreSQL to retrieve all the team's players, along with the team itself and the coach:
SELECT
"team".*,
( SELECT coach FROM "coach" WHERE "coach"."id" = "team".coach_id ) AS coach,
( SELECT ARRAY_AGG ( player ) FROM "player" WHERE "player".team_id = "team"."id" ) AS players,
FROM
"team"
WHERE
"team"."id" = '123'
This is working amazing!
But now I need to query the main_skill of the player for those players in ARRAY_AGG.
How to do that?
This is an SQL query I'm generating in my backend based on possibly asked graphql fields.
The actual result is:
| id | name | coach_id | coach | players |
|---|---|---|---|---|
| 1 | The good ones | 1 | {1,"Bob"} | {"(1,"John",1,1)","(2,"Tom",1,2)"} |
The result I need is:
| id | name | coach_id | coach | players |
|---|---|---|---|---|
| 1 | The good ones | 1 | {1,"Bob"} | {"(1,"John",1,1,{main_skill:{1,"MainSkill1Name"}})","(2,"Tom",1,2,{main_skill:{2,"MainSkill2Name"}})"} |
If you really need a json object inside
playersarray column you can use this query:Details:
json_build_object()function, and returns all the necessary in aplayer_datatable:player_dataresults are aggregated into one array in the outer query.See the demo.