Activity Forums Salesforce® Discussions How To Write The “where” Clause In Soql When Group By Is Used in Salesforce?

  • shariq

    Member
    September 20, 2018 at 2:58 pm

    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

  • Parul

    Member
    September 20, 2018 at 4:45 pm

    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

  • Avnish Yadav

    Member
    September 30, 2018 at 5:09 am

    Hello,

    We using HAVING clause insted of WHERE clause in GROUP BY

    Thanks.

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos