I am trying to isolate those records of [table] which only have specific (let's say bad) attributes. Bad attributes are those with ids 3, 6 and/or 7. To do that, I came up with the following solution:
SELECT * FROM (SELECT DISTINCT x, y, id_attribute FROM [table])
HAVING SUM(CASE WHEN id_attribute IN (3, 6, 7) THEN 0 ELSE 1 END) = 0
and this works fine. I would now like to take this one step further and remove the hardcoding on the (3, 6, 7) since the bad attributes might change. So, instead, I want to replace (3, 6, 7) with (SELECT DISTINCT id_attribute from [other_table] where description = 'bad').
But, when I put the two together, my query fails with the message:
Cannot perform an aggregate function on an expression containing an aggregate or a subquery.
Is there another way to do this?
You can use
Left Joinfor isolating those bad attributes: