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

Aggregate Results AVG not calculating correctly.

Hi All,


I have an aggregateResult query, which calculates the AVG of 'Segment' value (a field).  


There are 4 records with Segment values as 100, 0, 0, 0.  Ideally, the average should be 25 (100+0+0+0/4).  But, the query is calculating the avegare as100.  Since the values are 0s for 3 records, the query is not considering them. It is taking as 100/1= 100.


When the records have values, it is working fine. Ex: 100,50,50,100 - The avergare is correctly showing as 75.


How do I make it to display correct Average?  Below is the query.


AggregateResult[] groupedResults    = [Select Customer_Name__c,AVG(Segment__c) FROM Customer__c GROUP BY Customer_Name__c];


Please help.  Thanks.

sushant sussushant sus
Remove group by customer_name_c
then this will show exact result now it grouping by name then calculating avg
Thanks. But the Average has to be calculated for each grouping. So I added that GROUP BY criteria. If I remove that, the average will be only one value, which is not my requirement.

Need average for each grouping of Customer Name.