1

I have 3 tables:

  1. Scorecard
  2. Group
  3. Scorecard_Group (Joint table)

I am trying to write a query that fulfills the following purpose:

  1. Get the list of distinct scorecard ids WHERE Scorecard_Group.groupId IN (Pass array of groupIds)
  2. 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")
Erwin Brandstetter
  • 605,456
  • 145
  • 1,078
  • 1,228
Charles Semaan
  • 304
  • 2
  • 13

1 Answers1

1

Assuming a proper many-to-many implementation like:

Seems like you twisted / over-complicated your objective as well as your query.

SELECT DISTINCT s.id, s.name
FROM   scorecard_group sg
JOIN   scorecard       s  ON s.id = sg.scorecard_id
WHERE  sg.group_id = ANY ('{1,2,3}')  -- your array of group_id's here!
ORDER  BY 1, 2;  -- optional order

This retrieves the full, distinct set of scorecards (id, name) that are member in the given groups (array of group_id's).

Or, the same a bit faster for many duplicate memberships:

SELECT s.scorecard_id, s.name
FROM   scoeguard s
JOIN  (
   SELECT DISTINCT sg.scorecard_id
   FROM   scorecard_group sg
   WHERE  sg.group_id = ANY ('{1,2,3}')  -- your array of group_id's here!
   ) sg ON sg.scorecard_id = s.id
ORDER  BY 1, 2;  -- optional order

Using legal, lower-case, unquoted identifiers like you should, too. See:

To use IN, you'd have to provide a set or list.
Use = ANY for an array. See:

Erwin Brandstetter
  • 605,456
  • 145
  • 1,078
  • 1,228
  • Indeed I have over complicated and knew that it could be done in a more efficient manner given that I have made the many to many relationship. Much appreciated for the answer and the insight regarding IN and ANY. – Charles Semaan Jun 06 '22 at 23:22
  • Mind if I ask out of curiosity what was wrong in my query in case I needed to use the values of the subquery in another use case later. – Charles Semaan Jun 07 '22 at 00:31
  • 1
    @charleslenx: You cannot reference an output column generated in the `SELECT` list (`"scIds"`) from a `WHERE` clause, which is applied *before* computing the `SELECT` list. Consider the *"Sequence of events in a SELECT query"* here: https://stackoverflow.com/a/8242764/939860 – Erwin Brandstetter Jun 07 '22 at 00:40