+ Start a Discussion

help with SOQL aggregate funtions

I have two objects Portfolio__c with a formula filed  Index__r.Chg_Pct__c and a child object position(s) with a field



I want the SUM(Daily_Perf__c ) from positions while selecting each portfolio object


when I executed the following query

select  Index__r.Chg_Pct__c  , (select SUM(Daily_Perf__c) from positions__r)  from Portfolio__c


I get the error "Only root querires support aggregate expressions"


If  I go from child and use Group BY



select SUM(Daily_Perf__c) dailypref, Portfolio__r.Index__r.Chg_Pct__c  from position__c
group by  Portfolio__r.Index__r.Chg_Pct__c


I get the error "field Chg_Pct__c cannot be grouped in a query call . Is it becuase its a formulat filed ?


How do I  do SOQL simillar to Rollup summary Filed in salesforce ?




Ankit AroraAnkit Arora

You can not use aggregate query as inquery because it returns List<AggregateResults> and SOQL query returns List<SObject> or map<Id, SObject>.


Also you can only group the fields those have "groupable=true" in query call. You need to fetch the aggregate result first and then use the result in your query.



Ankit Arora

Blog | Facebook | Blog Page


Is there any way to make a field groupable and is there any documentation on what can and can't be groupable? 


Thanks! :)