2

I want to use group by from salesforce query :

SELECT  Candidat__c, Commande__c,Statut__c,IsDeleted, TypeContrat__c 
FROM Candidature__c 
WHERE Candidat__c = '0032000001GAPoXAAX' 
AND  Statut__c = 'Acceptée'
GROUP BY Commande__c

As response i have :

MALFORMED_QUERY : Field myst be grouped or aggregated : Candidat__c

So i did :

SELECT  Candidat__c, Commande__c,Statut__c,IsDeleted, TypeContrat__c 
FROM Candidature__c 
WHERE Candidat__c = '0032000001GAPoXAAX' 
AND  Statut__c = 'Acceptée'
GROUP BY Commande__c, Candidat__c

I get the same error.

MALFORMED_QUERY : Field myst be grouped or aggregated : Statut__c

So how if i have many fields ? i have to group by of each field ?

Thank you.

1
  • you can add them in where condition too like Statut__c != null. May 9, 2016 at 15:49

2 Answers 2

3

You have to Group By all the non-aggregate functions. Aggregate Functions are those such as AVG(), COUNT(), MIN(), MAX(), SUM()

So an example would be:

SELECT  Candidat__c, Commande__c,Statut__c,IsDeleted, TypeContrat__c, Count(ID)
FROM Candidature__c 
WHERE Candidat__c = '0032000001GAPoXAAX' 
AND  Statut__c = 'Acceptée'
GROUP BY Candidat__c, Commande__c,Statut__c,IsDeleted, TypeContrat__c
1

No, you don't need to group each field used in SOQL select command.

You have only two options available in aggregate SOQL Queries (Either of two) :-

  1. You must aggregate the field using aggregate functions (like AVG(field_name), MAX(field_name), MIN(field_name), SUM(field_name) )

OR

  1. Use the field in GROUP BY clause of the SOQL Query. Grouping field can not be used in aggregate function.

Reason:-

Grouping field affects other fields to show the data as they are collapsed while grouping.

For e.g : I want to show the "Name" of the Opportunity by grouping "StageName" field in SOQL.

[ Select Name From Opportunity GROUP BY StageName ] // NOT GOOD

It wont be possible because there will be lot of names per each unique stage which can not fit in one column of each row. We can only calculate the number of Opportunities for each stage using Count() aggregate functions.

[Select Count(Name) From Opportunity GROUP BY StageName ] ;

OR

[Select Name From Opportunity GROUP BY StageName,Name  ] ;

Thanks Shanu

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .