November 19, 2016 by LanceShi
2.6 Group by considerations
There are several considerations for using Group by clause. Let’s take a look at them.
First, all fields are created equal. But some fields are more equal than the others. In regards to group by clause, it means not all fields are groupable.
You can find out which fields are groupable by looking at the groupable field in DescribeSObjectResult class. However, the official document doesn’t tell what makes a field groupable and which are not. There is a discussion about this topic in SFSE (Salesforce Stackexchange). The link is here.
By current experiments, it seems that you can’t group by custom number fields and formula fields. As a walkaround, you can create a custom text fields and use work flow or trigger to copy the value to it.
Second, if you want to use Aggregate funcitons (Max, Sum, Avg, etc.) and limit clause in the same SOQL query, you MUST use a group by.
So the following query in invalid:
Select Max(CreatedDate) From Account Limit 1
Third, you can group by a child relationship field.
We will talk more about child relationship query in later posts