How to avoid error "aggregate functions are not allowed in WHERE"
Solution 1:
Replace WHERE
clause with HAVING
, like this:
SELECT o.ID , count(p.CAT)
FROM Orders o
INNER JOIN Products p ON o.P_ID = p.P_ID
GROUP BY o.ID
HAVING count(p.CAT) > 3;
HAVING
is similar to WHERE
, that is both are used to filter the resulting records but HAVING
is used to filter on aggregated data (when GROUP BY
is used).
Solution 2:
Use HAVING
clause instead of WHERE
Try this:
SELECT o.ID, COUNT(p.CAT) cnt
FROM Orders o
INNER JOIN Products p ON o.P_ID = p.P_ID
GROUP BY o.ID HAVING cnt > 3