You need to sign in to do that
Don't have an account?

Sum a SubQuery in SOQL
Trying to come up with an SOQL query to sum last months sales of specific items. In short, they need to be in our "Reporting" company vertical, but not include some products.
I'm new to SOQL, but experienced with SQL...which well, is a bit frustrating, because I could do this in my sleep in SQL.
So, I have this query:
SELECT
(SELECT TotalPrice FROM OpportunityLineItems WHERE (not PriceBookEntry.Product2.Name like '%ancillary%') and (not PriceBookEntry.Product2.Name like '%listen%') )
FROM
Opportunity
WHERE
CloseDate = LAST_MONTH AND
RecordType.Name = 'Reporting' AND
StageName = 'Closed Won' AND
Id NOT IN (SELECT OpportunityId FROM OpportunityLineItem WHERE PriceBookEntry.Product2.Name like '%ancillary%' or PriceBookEntry.Product2.Name like '%listen%' )
But, it's returning a dataset with an array. I've tried doing a sum around the subquery, and I get an error (MALFORMED_QUERY).
How do I collapse the array to get a sum of the LineItems for those products?
Thanks.
I'm new to SOQL, but experienced with SQL...which well, is a bit frustrating, because I could do this in my sleep in SQL.
So, I have this query:
SELECT
(SELECT TotalPrice FROM OpportunityLineItems WHERE (not PriceBookEntry.Product2.Name like '%ancillary%') and (not PriceBookEntry.Product2.Name like '%listen%') )
FROM
Opportunity
WHERE
CloseDate = LAST_MONTH AND
RecordType.Name = 'Reporting' AND
StageName = 'Closed Won' AND
Id NOT IN (SELECT OpportunityId FROM OpportunityLineItem WHERE PriceBookEntry.Product2.Name like '%ancillary%' or PriceBookEntry.Product2.Name like '%listen%' )
But, it's returning a dataset with an array. I've tried doing a sum around the subquery, and I get an error (MALFORMED_QUERY).
How do I collapse the array to get a sum of the LineItems for those products?
Thanks.
AggregateResult[] groupedResults
= [SELECT SUM(TotalPrice) FROM OpportunityLineItems WHERE (not PriceBookEntry.Product2.Name like '%ancillary%')
AND (not PriceBookEntry.Product2.Name like '%listen%')
AND Opportunity.StageName = 'Closed Won'
AND Opportunity.RecordTypeId = Schema.getGlobalDescribe().get('Opportunity').getDescribe().getRecordTypeInfosByName().get('Reporting').getRecordTypeId()
AND Opportunity.CloseDate > System.today().AddMonths(-1)];
for (AggregateResult ar : groupedResults) {
System.debug('TotalPrice' + ar.get('expr0'));
}
Replace your code with above Query and see the debug log, you will get the expected output............
All Answers
You need to use SOQL Aggregate Functions to do the SUM.
Like :
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_SOQL_agg_fns.htm
https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_select_agg_functions.htm
Thanks
Shashikant
SELECT Sum(SELECT TotalPrice FROM OpportunityLineItems WHERE (not PriceBookEntry.Product2.Name like '%ancillary%') and (not PriceBookEntry.Product2.Name like '%listen%') ) FROM Opportunity WHERE CloseDate = LAST_MONTH AND RecordType.Name = 'Reporting' AND StageName = 'Closed Won' AND Id NOT IN (SELECT OpportunityId FROM OpportunityLineItem WHERE PriceBookEntry.Product2.Name like '%ancillary%' or PriceBookEntry.Product2.Name like '%listen%' )
And Neither is this:
SELECT (SELECT Sum(TotalPrice) FROM OpportunityLineItems WHERE (not PriceBookEntry.Product2.Name like '%ancillary%') and (not PriceBookEntry.Product2.Name like '%listen%') ) FROM Opportunity WHERE CloseDate = LAST_MONTH AND RecordType.Name = 'Reporting' AND StageName = 'Closed Won' AND Id NOT IN (SELECT OpportunityId FROM OpportunityLineItem WHERE PriceBookEntry.Product2.Name like '%ancillary%' or PriceBookEntry.Product2.Name like '%listen%' )
AggregateResult[] groupedResults
= [SELECT SUM(TotalPrice) FROM OpportunityLineItems WHERE (not PriceBookEntry.Product2.Name like '%ancillary%')
AND (not PriceBookEntry.Product2.Name like '%listen%')
AND Opportunity.StageName = 'Closed Won'
AND Opportunity.RecordTypeId = Schema.getGlobalDescribe().get('Opportunity').getDescribe().getRecordTypeInfosByName().get('Reporting').getRecordTypeId()
AND Opportunity.CloseDate > System.today().AddMonths(-1)];
for (AggregateResult ar : groupedResults) {
System.debug('TotalPrice' + ar.get('expr0'));
}
Replace your code with above Query and see the debug log, you will get the expected output............
Now I just need to figure out why the numbers are wrong... I wish reports had a "export SOQL Query" button...