• About
  • Contact me!

Salesforce Everywhere

~ Blog on Salesforce Dev and admin stuff

Salesforce Everywhere

Category Archives: Aggregate functions

Tip#3 – Aggregate functions in SOQL

28 Friday Aug 2015

Posted by Mani in Aggregate functions, Apex, SOQL

≈ 1 Comment

Tags

Aggregate functions in SOQL

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:
AggregateResult[] groupedResultsBillable;
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:
Decimal dur;
for (AggregateResult ar : groupedResultsBillable) //loop through the results
{
dur=(Decimal)ar.get(‘expr0’);
}
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.

Blog Stats

  • 146,257 hits

Archives

Recent Posts: Salesforce Everywhere

Expert Salesforce Solutions from a Turnaround Master :)

Einstein GPT

How to get field values on onLoad in lightning-record-edit-form in LWC?

Using Dynamic Actions

How to check if the logged in user is a Guest user or not in APEX class?

Connect with me

  • View @manibalan_s’s profile on Twitter
  • LinkedIn

Follow this Blog via Email

Enter your email address and click on Follow us (below) to follow this blog and receive notifications of new posts by email.

Powered by WordPress.com.

 

Loading Comments...