Note: This release is in preview. Features described here don’t become generally available until the latest general availability date that Salesforce announces for this release. Before then, and where features are noted as beta, pilot, or developer preview, we can’t guarantee general availability within any particular time frame or at all. Make your purchase decisions only on the basis of generally available products and features.
group-by rollup
Syntax
1group data_stream by rollup(fields);| Parameter | Description |
|---|---|
| data_stream | Data input to group. |
| fields | Fields by which data is grouped. |
rollup supports the following aggregate functions.
- average()
- count()
- min()
- max()
- sum()
- unique()
1q = load "Superstore";
2q = group q by rollup('Category', 'Sub_Category');
3q = order q by ('Category');
4q = foreach q generate 'Category' as 'Category', 'Sub_Category' as 'Sub_Category', sum('Sales') as 'sum_sales';| Category | Sub-Category | sum_sales |
|---|---|---|
| Furniture | Bookcases | 114,348 |
| Chairs | 328,237 | |
| Furnishings | 91,514 | |
| Tables | 206,966 | |
| - | 741,064 | |
| Office Supplies | Appliances | 107,532 |
| Art | 27,119 | |
| Binders | 203,413 | |
| Envelopes | 16,363 | |
| Fasteners | 3,024 | |
| Labels | 12,486 | |
| Paper | 78,479 | |
| Storage | 223,844 | |
| Supplies | 46,674 | |
| - | 718,934 | |
| Technology | Accessories | 167,380 |
| Copiers | 149,528 | |
| Machines | 189,239 | |
| Phones | 329,636 | |
| - | 835,783 | |
| - | 2,295,781 |
The query first groups the total sales for each sub-category of a given category. Next, it groups the total sales for a single category. After each category's total sales is accounted for, the query generates the total sales for all categories.
rollup with Null Values
To return grouped null values in your queries, you must select the null handling for dimensions preference in Setup. See group-by for more information.
This example shows how null values display in query results. The query is the same as the one in the first example.
1q = load "Superstore";
2q = group q by rollup('Category', 'Sub_Category');
3q = foreach q generate 'Category' as 'Category', 'Sub_Category' as 'Sub_Category', sum('Sales') as 'sum_sales';
4q = order q by ('Category', 'Sub_Category');| Category | Sub-Category | sum_sales |
|---|---|---|
| Furniture | Bookcases | 114,348 |
| Chairs | 328,237 | |
| Furnishings | 91,514 | |
| Tables | 206,966 | |
| - | 92 | |
| - | 741,156 | |
| Office Supplies | Appliances | 107,532 |
| Art | 27,119 | |
| Binders | 203,413 | |
| Envelopes | 16,363 | |
| Fasteners | 3,024 | |
| Labels | 12,486 | |
| Paper | 78,479 | |
| Storage | 223,844 | |
| Supplies | 46,674 | |
| - | 273 | |
| - | 719,206 | |
| Technology | Accessories | 167,380 |
| Copiers | 149,528 | |
| Machines | 189,239 | |
| Phones | 329,636 | |
| - | 259 | |
| - | 836,041 | |
| - | Computers | 113 |
| Projectors | 744 | |
| - | 562 | |
| 1,420 | ||
| 2,297,824 |
The query first groups the total sales for each sub-category of a given category. In this example, each category contains a null sub-category. The value of the null sub-category is also included in the total sales for each sub-category.
After the query accounts for all of the named categories—categories that have a value—it displays the sub-categories and total sales for null categories. Finally, the query generates the total sales for all categories.
rollup with Null Values and case Statements
1q = load "Superstore";
2q = group q by rollup ('Category', 'Sub_Category');
3q = foreach q generate
4(case
5 when grouping('Category') == 1 then "All Categories"
6 else 'Category'
7end) as 'Category',
8(case
9 when grouping('Sub_Category') == 1 then "All Sub-Categories"
10 else 'Sub_Category'
11end) as 'SubCategory', sum('Sales') as 'sum_sales';| Category | Sub-Category | sum_sales |
|---|---|---|
| Furniture | Bookcases | 114,348 |
| Chairs | 328,237 | |
| Furnishings | 91,514 | |
| Tables | 206,966 | |
| - | 92 | |
| All Sub-Categories | 741,156 | |
| Office Supplies | Appliances | 107,532 |
| Art | 27,119 | |
| Binders | 203,413 | |
| Envelopes | 16,363 | |
| Fasteners | 3,024 | |
| Labels | 12,486 | |
| Paper | 78,479 | |
| Storage | 223,844 | |
| Supplies | 46,674 | |
| - | 273 | |
| All Sub-Categories | 719,206 | |
| Technology | Accessories | 167,380 |
| Copiers | 149,528 | |
| Machines | 189,239 | |
| Phones | 329,636 | |
| - | 259 | |
| All Sub-Categories | 836,041 | |
| - | Computers | 113 |
| Projectors | 744 | |
| - | 562 | |
| All Sub-Categories | 1,420 | |
| All Categories | All Sub-Categories | 2,297,824 |