In this post, we will see SOQL Aggregate Functions in Apex Programming Language | Salesforce Development
Program Code 1 (Anonymous Window):
//Without Aliases
AggregateResult[] records=[SELECT LeadSource, AVG(Amount), MAX(Amount) FROM Opportunity GROUP BY LeadSource];
for(AggregateResult record: records)
{
System.debug('Lead Source:'+record.get('LeadSource')+' Avg Amount: '+record.get('expr0')+' Max Amount: '+record.get('expr1'));
}
Program Code 2 (Anonymous Window):
//With Aliases
AggregateResult[] records=[SELECT LeadSource, AVG(Amount) avgamount, MAX(Amount) maxamount FROM Opportunity GROUP BY LeadSource];
for(AggregateResult record: records)
{
System.debug('Lead Source:'+record.get('LeadSource')+' Avg Amount: '+record.get('avgamount')+' Max Amount: '+record.get('maxamount'));
}
Program Code 3 (Anonymous Window):
//SOQL For Loop
for(AggregateResult record: [SELECT LeadSource, AVG(Amount) avgamount, MAX(Amount) maxamount FROM Opportunity GROUP BY LeadSource])
{
System.debug('Lead Source:'+record.get('LeadSource')+' Avg Amount: '+record.get('avgamount')+' Max Amount: '+record.get('maxamount'));
}
No comments:
Post a Comment