Aggregate functions in SOQL:
Aggregate functions in SOQL, such as SUM(),MAX(),COUNT() and AVG() are used to summarise the data in a query.
Suppose,we want to sum the Duration of all the Billable Jobs on a custom object Job, the query can be as follows:
groupedResultsBillable=[select sum(Duration__c) from Job__c where Job_Billable__c=true];
Note that any query that includes an aggregate function returns its results in an array of AggregateResult objects.
Get the sum of the duration using the following code:
for (AggregateResult ar : groupedResultsBillable) //loop through the results
Note: Any aggregated field in a SELECT list that does not have an alias automatically gets an implied alias with a format expri, where i denotes the order of the aggregated fields with no explicit aliases. The value of i starts at 0 and increments for every aggregated field with no explicit alias.