Error: Field must be grouped or aggregated

  • We have a lookup relationship between a custom object Problem__c and cases, one problem can be related to many cases. I am trying to query the field or problem records and the number of related cases, this is working partially by doing this.

    select problem__r.name,count(casenumber) from case where problem__c!=null group by problem__r.name limit 10
    

    The problem is that I need to get more fields from the problem object so I tried this

    select problem__r.name,problem__r.status__c,count(casenumber) from case where problem__c!=null group by problem__r.name limit 10
    

    But I get the error Error: Field must be grouped or aggregated Status__c. How can I retrieve more fields from the parent object using this query?

    Thanks.

  • Mike Chale

    Mike Chale Correct answer

    8 years ago

    When using aggregate functions like count, all other fields must be grouped. Try

    select problem__r.name,problem__r.status__c,count(casenumber) 
    from case where problem__c!=null 
    group by problem__r.name, problem__r.status 
    limit 10
    

    This approach actually worked.

    This helped me out to Mike!

License under CC-BY-SA with attribution


Content dated before 7/24/2021 11:53 AM