Grouping Examples

AVAILABLE API VERSION
API v58.0 and later

You can use aggregate functions without a groupBy argument or use them with a groupBy argument with optional types CUBE or ROLLUP.

Use the aggregate function with a groupBy argument to find the average amount of all your opportunities by campaign.

This query returns the average amount of all your opportunities by campaign.

The previous query is similar to the following SOQL statement.

You can also use the aggregate function with a groupBy argument to group by multiple fields.

This query is similar to the following SOQL statement.

This query provides the following response.

The previous query is similar to the following SOQL statement.

This query provides the following response.

The previous query is similar to the following SOQL statement.

This query provides the following response.

The previous query is similar to the following SOQL statement.

SOQL and SOSL Reference: GROUP BY CUBE