I have the following query which works fine:
Select
SUM(CASE
WHEN MethodId NOT IN (1,2) THEN 1
ELSE 0
END) Total
FROM pll_data
Next, I need to check if data exist in another table for the same MethodId. I get the following error (Cannot perform an aggregate function on an expression containing an aggregate or a subquery.
) when I add the EXISTS clause that preferences another table:
SELECT
SUM(CASE
WHEN
EXISTS(Select 1 from pll_dataSys pp WHERE pp.PNum = p.PNum AND pp.MethodID IN (1,2,3)) OR -- I added this line of code
MethodId NOT IN (1,2) THEN 1
ELSE 0
END) Total
FROM pll_data p
NOTE: I get the following error when I add the EXISTS clause:
Cannot perform an aggregate function on an expression containing an aggregate or a subquery.