function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
aolivenciaaolivencia 

Query error "Field must be grouped or aggregated: Id"

Hi, I'm getting a strange error when I try to save my class file:

 

Field must be grouped or aggregated: Id

 

The error is somewhere here:

 

 

for (AggregateResult iter : [
Select
Id,
SUM(ReleaseAmount__c)

from myObj__c Where nObj__c = 'a0DA00000012fbMMAQ'
]) { .........................

 

Any idea ?



 

Message Edited by aolivencia on 03-17-2010 01:56 PM
Best Answer chosen by Admin (Salesforce Developers) 
SuperfellSuperfell
Yes, your query is not valid, if you're summing releaseAmount__c, which id are you expecting to get returned ? At a guess, in your case you can just remove Id from the select list.

All Answers

SuperfellSuperfell
Yes, your query is not valid, if you're summing releaseAmount__c, which id are you expecting to get returned ? At a guess, in your case you can just remove Id from the select list.
This was selected as the best answer
yvk431yvk431

You just need to use the agrregate function as the error describes

 

Select Id,SUM(ReleaseAmount__c) from myObj__c Where nObj__c = 'a0DA00000012fbMMAQ' group by Id

 

That is it. Hope this is what you are looking for

 

aolivenciaaolivencia

Thanks.

 

Documentation says: "You can use these functions without using a GROUP BY clause" : http://www.salesforce.com/us/developer/docs/api/Content/sforce_api_calls_soql_select_agg_functions.htm

 

Maybe it works just with one aggregated fetched field 

SuperfellSuperfell
Yes, if all the fields are aggregated, you don't need a group by, but not all your query fields were aggregated, i suspect you're trying to sum up all the record for that particular foreign key value, in which case, remove the id from the select list as i initially suggested.