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
Nathan PratsNathan Prats 

Sum multiple SOQL Queries Results

Hi, 

I'd like to sum the results of 2 queries. I'd use those results in order to double check some figures. 
I pasted the two queries herebelow, they have different timeframe.

SELECT Sum(Power_of_1__c)
FROM Task
WHERE CallType = 'Outbound'
AND Sucessful_call__c = TRUE
AND CreatedDate >= LAST_N_WEEKS: 4

SELECT Sum(Power_of_1__c)
FROM Task
WHERE CallType = 'Outbound'
AND Sucessful_call__c = TRUE
AND CreatedDate >= LAST_N_MONTHS: 4

Any help would be much appreciated. 

Nathan
Siddharth ManiSiddharth Mani
Assuming that you need the Integer sum, you can try the below:
List<AggregateResult> a = [SELECT Sum(Power_of_1__c) week
FROM Task
WHERE CallType = 'Outbound'
AND Sucessful_call__c = TRUE
AND CreatedDate >= LAST_N_WEEKS: 4];

Integer weekValue = Integer.valueOf((a[0].get('week')));


List<AggregateResult> b = [SELECT Sum(Power_of_1__c) month
FROM Task
WHERE CallType = 'Outbound'
AND Sucessful_call__c = TRUE
AND CreatedDate >= LAST_N_MONTHS: 4];

Integer monthValue = Integer.valueOf((b[0].get('month')));

Integer sum = weekValue+monthValue;

 
Nathan PratsNathan Prats
Hi Siddharth, 

Unfortunately, I get this error : "MALFORMED_QUERY: unexpected token: List"
I use Force.com explorer and the Query i'm trying to build is not part of an apex code. 
I'd use this Query in Conga Composer to fill an Excel Spreadsheet. 
What I'm trying to do might not be possible I'm afraid. 

Nathan.
Pankaj_GanwaniPankaj_Ganwani
Hi Nathan,

Please use below mentioned code:
 
AggregateResult a = [SELECT Sum(Power_of_1__c) week
FROM Task
WHERE CallType = 'Outbound'
AND Sucessful_call__c = TRUE
AND CreatedDate >= LAST_N_WEEKS: 4];

Integer weekValue = Integer.valueOf((a.get('week')));


AggregateResult b = [SELECT Sum(Power_of_1__c) month
FROM Task
WHERE CallType = 'Outbound'
AND Sucessful_call__c = TRUE
AND CreatedDate >= LAST_N_MONTHS: 4];

Integer monthValue = Integer.valueOf((b.get('month')));

Integer sum = weekValue+monthValue;

 
JeffreyStevensJeffreyStevens
Are you sure you can do an AggregateResult SOQL in force.com explorer?  I know you can't do one in eclipse or in the developer console explorer.
Nathan PratsNathan Prats
Hello Jeffrey, 

I don't think that we can do an AggregateResult SOQL in force.com explorer. I'm looking for a work around. 
Pankaj_GanwaniPankaj_Ganwani
We can do this in Developer Console.
JeffreyStevensJeffreyStevens
Ah - yes - you're correct.  In Execute Anonymous - yes - And in the Query Editor - just using the format of SELECT id(count) idCount FROM Account format.