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.

Answer

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

Attribution
Source : Link , Question Author : PepeFloyd , Answer Author : Mike Chale

Leave a Comment