I have 3 tables:
- Scorecard
- Group
- Scorecard_Group (Joint table)
I am trying to write a query that fulfills the following purpose:
- Get the list of distinct scorecard ids WHERE Scorecard_Group.groupId IN (Pass array of groupIds)
- Get all Scorecard."name" where Scorecard."id" IN (Array of scorecardIds that we just queries)
I am able to achieve this using 2 queries but I would like for an optimal solution. Here is my 2 queries solution:
SELECT array_agg(DISTINCT sg."scorecardId") as "ids"
FROM scorecard_group sg
WHERE sg."groupId" IN (${groupIds.concat()})
SELECT sc."id", sc."name"
FROM "scorecard" sc
WHERE sc."id" IN (${scIds[0].ids.concat()})
I tried to achieve the same result in 1 query but I am getting:
column "scIds" does not exist
SELECT sc."name", sc."id",
(
SELECT DISTINCT sg."scorecardId"
FROM scorecard_group sg
WHERE sg."groupId" IN (${groupIds.concat()})
) as "scIds"
FROM "scorecard" sc
WHERE sc."id" IN ("scIds")