SOQL Aggregate  Functions:

“SOQL Aggregate Functions” shows a calculated result from the query and returns AggregateResult.

Any query that includes an aggregate function returns its results in an array of AggregateResult objects. AggregateResult is a read-only sObject and is only used for query results.

1. Sum():

AggregateResult ar = [SELECT SUM(Max_Salary__c) 

FROM Position__c WHERE Max_Salary__c > 10000];

2. Max():

AggregateResult ar = [SELECT MAX(Max_Salary__c)

FROM Position__c 

WHERE Max_Salary__c > 10000];

3. Min():

AggregateResult ar = [SELECT MIN(Max_Salary__c)

FROM Position__c 

WHERE Max_Salary__c];

4. Count() and Count(fieldName):

Integer i = [SELECT count(ID) FROM Account];

5. Avg():

AggregateResult ar = [SELECT AVG(Min_Salary__c)

FROM Position__c 

WHERE Max_Salary__c < 5000];

6. Count_Distinct():

AggregateResult ar = [SELECT COUNT_DISTINCT(Name) 

FROM Position__c];

Our Recent Blog

Share This Post