Activity Forums Salesforce® Discussions How to write the “Where” clause in SOQL when GroupBy is used for aggregate functions in Salesforce?

  • shariq

    Member
    September 22, 2018 at 10:01 pm

    We cannot use the “Where” clause with GroupBy for aggregate functions like SUM() 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%'

  • Parul

    Member
    September 23, 2018 at 4:19 am

    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

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos