Can an aggregate function be in SOQL where clause? Looks like it cannot but I can't find anywhere a documentation a clear answer if it is forbidden and why if it is
1 Answer
Look at the documentation around the HAVING clause.
Example:
SELECT Account.Owner.Name
FROM Opportunity
GROUP BY Account.Owner.Name
HAVING COUNT(Id) > 100
-
Yes, I know about Having, I wonder why we can't just query SELECT Account.Owner.Name FROM Opportunity where COUNT(Id) > 100 Commented Sep 8, 2015 at 19:05
-
1@XpucTuHKaBuwuBaHKa HAVING applies to "groups" of rows, while WHERE applies to "individual" rows. This is how standard SQL works, and SOQL is a subset of this.– sfdcfox ♦Commented Sep 8, 2015 at 19:24
-
This is one of the really obscure questions that can absolutely nail someone taking a certification exam when they've never run across a situation where they needed to create a query like the one above. Commented Sep 8, 2015 at 22:51