---
schema_version: "1"
id: c9177d6e5ebfce565ab517ef83d18b1a275f4340ea92bfed98b6bd773b153c69
title: Group By Rollup
collection: developer
version: current
locale: en-us
portal_url: https://docs.salesforce.com/developer/current/en-us/graphql-api/graphql/guides/aggregate/aggregate-groupby-rollup.md
canonical_url: https://developer.salesforce.com/docs/platform/graphql/guide/aggregate-groupby-rollup.html
source_site: developer.salesforce.com
content_type: documentation
content_hash: 9c9cc35101a1f140600ec79bb16d34ebce459af93cf1b2a067db13c6026dfcba
products:
  - GraphQL API
---
# Group By Rollup

| AVAILABLE API VERSION |
| --------------------- |
| API v58.0 and later   |

The `ROLLUP` type adds subtotals for aggregate data in the query result. Use `ROLLUP` with [aggregate functions](https://developer.salesforce.com/docs/platform/graphql/guide/aggregate-support.md), such as `sum` and `count`.

## Aggregate Results with Group By Rollup

To aggregate results and include subtotals in the query result, use the following syntax. Replace _objectApiName_ with the object's API name, like Account. _Field1_ and _Field2_ represent fields on the object, like Industry and AnnualRevenue.

```
query rollupExample {
  uiapi {
    aggregate {
      objectApiName (groupBy: { type: ROLLUP, Field1: { group: true }, Field2: { group: true } }) {
        edges {
          node {
            aggregate {
              _Field1_ {
                value
                grouping {
                  value
                }
              }
              _Field2_ {
                value
                grouping {
                  value
                }
              }
              // Other optional fields
             }
          }
        }
    }
  }
}
```

A query that uses the `groupBy` argument with the `ROLLUP` type returns the same aggregated data as a query with the `groupBy` argument. However, a query with the `ROLLUP` type also returns multiple levels of subtotals. For example, you can return the number of records for each lead sources and a subtotal of those records using the `ROLLUP` type.

**note:** A query with the `ROLLUP` type can include up to three fields in a comma-separated list in the `groupBy` argument clause. The result includes one grand total regardless of how many rollup fields you use.

The order of rollup fields you pass in to the `groupBy` argument determines how the subtotals are calculated and how the results are grouped.

## Group By One Rollup Field

Grouping by one rollup field returns subtotals for each field selection and one grand total. For example, if you query the rollup by `LeadSource`, the query returns the number of records for each lead source and a grand total.

```
query LeadSourceRollupExample {
  uiapi {
    aggregate {
      Lead ( groupBy: { type: ROLLUP, LeadSource: { group: true } } ) {
        edges {
          node {
            aggregate {
              Name {
                count {
                  value
                }
              }
            }
          }
        }
      }
    }
  }
}
```

The previous query is similar to this SOQL statement.

```
SELECT LeadSource, COUNT(Name)
FROM Lead
GROUP BY ROLLUP(LeadSource)
```

## Group By Two Rollup Field

A query that includes two rollup fields returns first-level subtotals for _Field1_. Results are grouped by _Field2_. For example, if you query the rollup by `Status` and `LeadSource`, the query returns subtotals for the number of records for each lead source that has a particular status.

```
query LeadSourceExample {
  uiapi {
    aggregate {
      Lead ( groupBy: { type: ROLLUP, Status: { group: true }, LeadSource: { group: true } } ) {
        edges {
          node {
            aggregate {
              Name {
                count {
                  value
                }
              }
            }
          }
        }
      }
    }
  }
}
```

The previous query is similar to this SOQL statement.

```
SELECT Status, LeadSource, COUNT(Name)
FROM Lead
GROUP BY ROLLUP(Status, LeadSource)
```

## Group By Three Rollup Field

A query that includes three rollup fields returns:

- First-level subtotals for each combination of _Field1_ and _Field2_. Results are grouped by _Field3_.
- Second-level subtotals for each value of _Field1_. Results are grouped by _Field2_ and _Field3_.

## See Also

[SOQL and SOSL Reference: GROUP BY ROLLUP](https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_select_groupby_rollup.htm)
