[SalesForce] getting group by aggregate errors in this query

I need to count the total number of orgs. Why won't this query work? I get errors not matter what I group or not group. I only put in Group by to try to placate the compiler but he's in a very bad mood today. Without COUNT(ORG__C) and the group by clause it works.

Select BOG_Membership_Type_New__c,Contact_Name__c, ORG__c, COUNT(ORG__C)
FROM BOG_Membership__c 
WHERE BOG_Membership_Status__c = 'Active' 
AND BOG_Membership_Type_New__c <> ''
GROUP BY BOG_Membership_Type_New__c,Contact_Name__c, ORG__c
ORDER BY BOG_Membership_Type_New__c ,Contact_Name__c, ORG__c

Best Answer

The rule of thumb here is

Any field thats included in Aggregate SOQL has to be either aggregated or grouped

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

The final query will be

Select BOG_Membership_Type_New__c, Contact_Name__c,COUNT(ORG__C)
 FROM BOG_Membership__c 
 WHERE BOG_Membership_Status__c = 'Active' 
 AND BOG_Membership_Type_New__c <> ''
 GROUP BY BOG_Membership_Type_New__c, Contact_Name__c
 ORDER BY BOG_Membership_Type_New__c , Contact_Name__c
Related Topic