Query.apex

  • Docs
  • Tutorials
Docs Menu
  • Query class
    • Constructor
    • Result
    • Field Selection
    • Condition
    • Subquery
    • Aggregate Functions

Aggregate Functions Guide

Aggregate Functions in Query

This section describes methods related to aggregate functions. When using any of these methods, you can only get the result using the aggregate() method in the end.

count

Apply COUNT() function to a field.

public Query count(String field)

field: API name of the field. Can also be a field of a parent, e.g. 'Owner.Name'.

new Query('Account').
    count('Id');

public Query count(String field, String alias)

field: API name of the field. Can also be a field of a parent, e.g. 'Owner.Name'.

alias: The alias set to the aggregate function.

new Query('Account').
    count('Id', 'idCount');

countDistinct

Apply COUNT_DISTINCT() function to a field.

public Query countDistinct(String field)

field: API name of the field. Can also be a field of a parent, e.g. 'Owner.Name'.

new Query('Account').
    countDistinct('Id');

public Query countDistinct(String field, String alias)

field: API name of the field. Can also be a field of a parent, e.g. 'Owner.Name'.

alias: The alias set to the aggregate function.

new Query('Account').
    countDistinct('Id', 'idCount');

max

Apply MAX() function to a field

public Query max(String field)

field: API name of the field. Can also be a field of a parent, e.g. 'Owner.Name'.

new Query('Account').
    max('Id');

public Query max(String field, String alias)

field: API name of the field. Can also be a field of a parent, e.g. 'Owner.Name'.

alias: The alias set to the aggregate function.

new Query('Account').
    max('NumberOfEmployees', 'maxEmployees');

min

Apply MIN() function to a field

public Query min(String field)

field: API name of the field. Can also be a field of a parent, e.g. 'Owner.Name'.

new Query('Account').
    min('Id');

public Query min(String field, String alias)

field: API name of the field. Can also be a field of a parent, e.g. 'Owner.Name'.

alias: The alias set to the aggregate function.

new Query('Account').
    min('NumberOfEmployees', 'minEmployees');

avg

Apply AVG() function to a field

public Query avg(String field)

field: API name of the field. Can also be a field of a parent, e.g. 'Owner.Name'.

new Query('Account').
    avg('Id');

public Query avg(String field, String alias)

field: API name of the field. Can also be a field of a parent, e.g. 'Owner.Name'.

alias: The alias set to the aggregate function.

new Query('Account').
    avg('NumberOfEmployees', 'avgEmployees');

sum

Apply SUM() function to a field

public Query sum(String field)

field: API name of the field. Can also be a field of a parent, e.g. 'Owner.Name'.

new Query('Account').
    sum('Id');

public Query sum(String field, String alias)

field: API name of the field. Can also be a field of a parent, e.g. 'Owner.Name'.

alias: The alias set to the aggregate function.

new Query('Account').
    sum('NumberOfEmployees', 'sumEmployees');

groupBy

Add the field to the GROUP BY clause.

public Query groupBy(String fields)

field: API name of the field, or multiple field names separated by ','.

new Query('Account').
    groupBy('Name').
    groupBy('OwnerId, CreatedById');

public Query groupBy(Set fieldSet)

field: A set of fields.

new Query('Account').
    groupBy(new Set{'Name', 'CreatedById'});

selectField

Select a specific field and set an alias to it.

This method is only available when using aggregate functions.

public Query selectField(String field, String alias)

field: API name of the field. Can also be a field of a parent, e.g. 'Owner.Name'.

alias: Name of the alias.

Query query =
    new Query('Account').
    selectField('Name', 'myName').
    groupBy('Name');

public Query selectField(Schema.SObjectField field, String alias)

field: A Schema.SObjectField.

alias: Name of the alias.

Query query =
    new Query(Account.getSObjectType()).
    selectField(Account.Name, 'myName').
    groupBy('Name');

addHaving

Add a HAVING clause as conditions of the aggregated query.

public Query addHaving(Condition condition)

condition: A Query.Condition.

new Query('Account').
count('Name', 'countName').
groupBy('Rating').
addHaving(Query.conditionGe('Count(Name)', 2));

aggregate

Get the result of the aggregated query.

Can only be used with aggregate functions.

public List aggregate()

Get the result of the aggregated query.

The returned list is guaranteed to be non-empty.

new Query('Account').
    count('Name').
    aggregate();

Contribute on Github! Edit this section.