# 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](/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.

```sfdocs-code {"lang":"graphql", "title": "Group by rollup syntax", "src": "" }
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.

```sfdocs-code {"lang":"graphql", "title": "Aggregate lead sources with rollup", "src": "" }
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.

```sfdocs-code {"lang":"sql", "title": "SOQL statement with GROUP BY ROLLUP", "src": "" }
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.

```sfdocs-code {"lang":"graphql", "title": "Rollup on status and lead source", "src": "" }
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.

```sfdocs-code {"lang":"sql", "title": "SOQL statement with GROUP BY ROLLUP", "src": "" }
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)
