---
schema_version: "1"
id: 44f2ac09a1a603400a3d2c06194aa7b4421a5bbe8827bcf7d1c69647fed50573
title: GROUP BY ROLLUP
collection: developer
version: current
locale: en-us
portal_url: https://docs.salesforce.com/developer/current/en-us/analytics/bi_dev_guide_sql/guides/bi-sql-groupby-rollup.md
canonical_url: https://developer.salesforce.com/docs/analytics/bi-dev-guide-sql/guide/bi-sql-groupby-rollup.html
source_site: developer.salesforce.com
content_type: documentation
content_hash: a75cfe27c7b88a8847c1a54053eb7c264a6c406b9aaa154468f3a0e663f4969e
products:
  - SQL for Analytics Developer Guide
  - analytics
---

# GROUP BY ROLLUP

`ROLLUP` is a sub-clause of `GROUP BY` that creates and displays aggregations of column data. The results output of `ROLLUP` is based on column order in your query.

`ROLLUP` supports the following aggregate functions.

- Average
- Count
- Min
- Max
- Sum

This example first groups the results by category and sub-category, and runs `Sum(Profit)`, an aggregate function on each resulting row. By modifying the`GROUP BY` clause with `ROLLUP`, the query "rolls up" the results into subtotals and grand totals.

```nolang
SELECT Category, Sub_Category, Sum(Profit) as TotalProfit
FROM "Superstore"
GROUP BY ROLLUP(Category, Sub_Category);
```

Notice how `GROUP BY ROLLUP (Category, Sub_Category)` creates groups for each column combination:

```nolang
Category, Sub-Category
Category
() — null
```

The query first groups the total profit for each subcategory of a given category. Next, it groups is the total profit for a single category. Once each category's total profit is accounted for, the query generates the total profit for all categories.

| Category        | Sub-Category | TotalProfit |
| --------------- | ------------ | ----------- |
| Furniture       | Bookcases    | -3,472.56   |
|                 | Chairs       | 25,590.17   |
|                 | Furnishings  | 13,059.14   |
|                 | Tables       | -17,725.48  |
|                 | -            | 18,451.27   |
| Office Supplies | Appliances   | 18,138.01   |
|                 | Art          | 6,527.79    |
|                 | Binders      | 30,221.76   |
|                 | Envelopes    | 6,964.18    |
|                 | Fasteners    | 949.52      |
|                 | Labels       | 5,546.25    |
|                 | Paper        | 34,053.57   |
|                 | Storage      | 21,278.83   |
|                 | Supplies     | -1,189.1    |
|                 | -            | 122,490.8   |

You can also group by multiple columns. This query generates the group of cities that contain stores in each category. The count column shows how many stores there are in each city.

```nolang
SELECT Category, City, count(*) as cnt
FROM "Superstore"
GROUP BY Category, City
```

Here are the first ten results.

| Category  | City         | cnt |
| --------- | ------------ | --- |
| Furniture | Akron        | 2   |
|           | Alexandria   | 3   |
|           | Allen        | 1   |
|           | Allentown    | 1   |
|           | Amarillo     | 4   |
|           | Anaheim      | 8   |
|           | Andover      | 1   |
|           | Apopka       | 1   |
|           | Apple Valley | 2   |
