I have a SOQL query that I'm trying to use the SUM function with. My issue is that the SUM function gives me a totally different result than if i just query the rows and add up the amounts
Query 1 - Selecting the rows individually
select id, Parts_Cost__c from customObject where Status= 'Completed'
and Contract = :contractId and Completed_On__c >= :contractStartDate
and Completed_On__c <= :contractEndDate
The individual results for parts cost sum to- 6773.59 and 17 rows are returned
Query 2- Aggregate Results:
select count(id), sum(Parts_Cost__c) from customObject where Status= 'Completed'
and Contract = :contractId and Completed_On__c >= :contractStartDate
and Completed_On__c <= :contractEndDate
This will give me a summed value of 8036.05 and the count result is 17 rows.
Can anyone think of reasons for these discrepancies in the results? Can I see how the 8036.05 is getting calculated?
contractId
,contractStartDate
, andcontractEndDate
). That would be the easiest explanation for the difference. When using aggregate functions, we lose the ability to see the individual records that were pulled by the query, so for purposes of testing it would make sense to hardcode values into your 3 variables there (alternatively, run both queries in the same transaction).