• 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

≈ Leave a 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.

Advertisement

Blog Stats

  • 114,771 hits

Archives

Recent Posts: Salesforce Everywhere

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?

How to Close Quick action in LWC

Lightning component to filter Products and display results in lightning data table

Connect with me

  • View @manibalan_s’s profile on Twitter
  • LinkedIn

Developer links

  • Salesforce Developer website
  • Salesforce Success Community

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

Website Powered by WordPress.com.

  • Follow Following
    • Salesforce Everywhere
    • Already have a WordPress.com account? Log in now.
    • Salesforce Everywhere
    • Customize
    • Follow Following
    • Sign up
    • Log in
    • Report this content
    • View site in Reader
    • Manage subscriptions
    • Collapse this bar