Activity › Forums › Salesforce® Discussions › How To Write The “where” Clause In Soql When Group By Is Used in Salesforce?
-
How To Write The “where” Clause In Soql When Group By Is Used in Salesforce?
Posted by Aman on September 20, 2018 at 2:57 PMHow To Write The “where” Clause In Soql When Group By Is Used?
Avnish Yadav replied 7 years, 7 months ago 4 Members · 3 Replies -
3 Replies
-
Hi,
We cannot use the “Where” clause with Group By instead we will need to use the “Having Clause“.
Example:
Get all the opportunity where more than one record exists with same name and name contains “ABC”.
SELECT COUNT(Id) , Name FROM Opportunity GROUP BY Name Having COUNT(Id) > 1 AND Name like ‘%ABC%’
Thanks
- [adinserter block='9']
-
Adding some points:
SELECT * FROM `movies` GROUP BY `category_id`,`year_released` HAVING `category_id` = 8;
The GROUP BY Clause is used to group rows with same values .
The GROUP BY Clause is used together with the SQL SELECT statement.
The SELECT statement used in the GROUP BY clause can only be used contain column names, aggregate functions, constants and expressions.
The HAVING clause is used to restrict the results returned by the GROUP BY clause.Thanks
-
Hello,
We using HAVING clause insted of WHERE clause in GROUP BY
Thanks.
Log In to reply.