Skip to main content

AGGREGATE FUNCTIONS

For more details check SOQL API - AGGREGATE FUNCTIONS.

NOTE! 🚨 All examples use inline queries built with the SOQL Lib Query Builder. If you are using a selector, replace SOQL.of(...) with YourSelectorName.query().

AVG​

SOQL

SELECT CampaignId, AVG(Amount) amount
FROM Opportunity
GROUP BY CampaignId

SOQL Lib

SOQL.of(Opportunity.SObjectType)
.with(Opportunity.CampaignId)
.avg(Opportunity.Amount, 'amount')
.groupBy(Opportunity.CampaignId)
.toAggregate();

SOQL

SELECT AVG(Opportunity.Amount)
FROM OpportunityLineItem

SOQL Lib

SOQL.of(OpportunityLineItem.SObjectType)
.avg('Opportunity', Opportunity.Amount)
.toAggregate();

SOQL

SELECT AVG(Opportunity.Amount) amount
FROM OpportunityLineItem

SOQL Lib

SOQL.of(OpportunityLineItem.SObjectType)
.avg('Opportunity', Opportunity.Amount, 'amount')
.toAggregate();

SUM​

SOQL

SELECT CampaignId, SUM(Amount) totalAmount
FROM Opportunity
GROUP BY CampaignId

SOQL Lib

SOQL.of(Opportunity.SObjectType)
.with(Opportunity.CampaignId)
.sum(Opportunity.Amount, 'totalAmount')
.groupBy(Opportunity.CampaignId)
.toAggregate();

SOQL

SELECT SUM(Opportunity.Amount)
FROM OpportunityLineItem

SOQL Lib

SOQL.of(OpportunityLineItem.SObjectType)
.sum('Opportunity', Opportunity.Amount)
.toAggregate();

SOQL

SELECT SUM(Opportunity.Amount) totalAmount
FROM OpportunityLineItem

SOQL Lib

SOQL.of(OpportunityLineItem.SObjectType)
.sum('Opportunity', Opportunity.Amount, 'totalAmount')
.toAggregate();

MIN​

SOQL

SELECT CampaignId, MIN(Amount) minAmount
FROM Opportunity
GROUP BY CampaignId

SOQL Lib

SOQL.of(Opportunity.SObjectType)
.with(Opportunity.CampaignId)
.min(Opportunity.Amount, 'minAmount')
.groupBy(Opportunity.CampaignId)
.toAggregate();

SOQL

SELECT MIN(Opportunity.Amount)
FROM OpportunityLineItem

SOQL Lib

SOQL.of(OpportunityLineItem.SObjectType)
.min('Opportunity', Opportunity.Amount)
.toAggregate();

SOQL

SELECT MIN(Opportunity.Amount) minAmount
FROM OpportunityLineItem

SOQL Lib

SOQL.of(OpportunityLineItem.SObjectType)
.min('Opportunity', Opportunity.Amount, 'minAmount')
.toAggregate();

MAX​

SOQL

SELECT CampaignId, MAX(Amount) maxAmount
FROM Opportunity
GROUP BY CampaignId

SOQL Lib

SOQL.of(Opportunity.SObjectType)
.with(Opportunity.CampaignId)
.max(Opportunity.Amount, 'maxAmount')
.groupBy(Opportunity.CampaignId)
.toAggregate();

SOQL

SELECT MAX(Opportunity.Amount)
FROM OpportunityLineItem

SOQL Lib

SOQL.of(OpportunityLineItem.SObjectType)
.max('Opportunity', Opportunity.Amount)
.toAggregate();

SOQL

SELECT MAX(Opportunity.Amount) maxAmount
FROM OpportunityLineItem

SOQL Lib

SOQL.of(OpportunityLineItem.SObjectType)
.max('Opportunity', Opportunity.Amount, 'maxAmount')
.toAggregate();

COUNT​

SOQL

SELECT CampaignId, COUNT(Id) totalRecords
FROM Opportunity
GROUP BY CampaignId

SOQL Lib

SOQL.of(Opportunity.SObjectType)
.with(Opportunity.CampaignId)
.count(Opportunity.Id, 'totalRecords')
.groupBy(Opportunity.CampaignId)
.toAggregate();

SOQL

SELECT COUNT(Opportunity.Id)
FROM OpportunityLineItem

SOQL Lib

SOQL.of(OpportunityLineItem.SObjectType)
.count('Opportunity', Opportunity.Id)
.toAggregate();

SOQL

SELECT COUNT(Opportunity.Id) totalRecords
FROM OpportunityLineItem

SOQL Lib

SOQL.of(OpportunityLineItem.SObjectType)
.count('Opportunity', Opportunity.Id, 'totalRecords')
.toAggregate();

COUNT_DISTINCT​

SOQL

SELECT COUNT_DISTINCT(AccountId) uniqueAccounts
FROM Opportunity

SOQL Lib

SOQL.of(Opportunity.SObjectType)
.countDistinct(Opportunity.AccountId, 'uniqueAccounts')
.toAggregate();

SOQL

SELECT COUNT_DISTINCT(Opportunity.AccountId)
FROM OpportunityLineItem

SOQL Lib

SOQL.of(OpportunityLineItem.SObjectType)
.countDistinct('Opportunity', Opportunity.AccountId)
.toAggregate();

SOQL

SELECT COUNT_DISTINCT(Opportunity.AccountId) uniqueAccounts
FROM OpportunityLineItem

SOQL Lib

SOQL.of(OpportunityLineItem.SObjectType)
.countDistinct('Opportunity', Opportunity.AccountId, 'uniqueAccounts')
.toAggregate();