group-by rollup

rollup is a subclause of group-by that creates and displays aggregations of grouped data. The output of rollup is based on column order in your query.

Syntax 

group-by rollup takes this syntax.

1group data_stream by rollup(fields);
ParameterDescription
data_streamData input to group.
fieldsFields by which data is grouped.

rollup works with group-by only. You cannot use it with cogroup.

Note

rollup supports the following aggregate functions.

  • average()
  • count()
  • min()
  • max()
  • sum()
  • unique()

This example first groups the results by Category and Sub-Category, and runs sum('Sales'), an aggregate function on each resulting row. By modifying the group-by clause with rollup, the query “rolls up” the results into subtotals and a grand total.

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';
CategorySub-Categorysum_sales
FurnitureBookcases114,348
 Chairs328,237
 Furnishings91,514
 Tables206,966
 -741,064
Office SuppliesAppliances107,532
 Art27,119
 Binders203,413
 Envelopes16,363
 Fasteners3,024
 Labels12,486
 Paper78,479
 Storage223,844
 Supplies46,674
 -718,934
TechnologyAccessories167,380
 Copiers149,528
 Machines189,239
 Phones329,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');
CategorySub-Categorysum_sales
FurnitureBookcases114,348
 Chairs328,237
 Furnishings91,514
 Tables206,966
 -92
 -741,156
Office SuppliesAppliances107,532
 Art27,119
 Binders203,413
 Envelopes16,363
 Fasteners3,024
 Labels12,486
 Paper78,479
 Storage223,844
 Supplies46,674
 -273
 -719,206
TechnologyAccessories167,380
 Copiers149,528
 Machines189,239
 Phones329,636
 -259
 -836,041
-Computers113
 Projectors744
 -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 

Use the grouping function and case statements together to label the subtotal and grand total categories. In this example, the first case checks for a null value generated by the rollup in the Category field. If true, then the query labels the field All Categories. The second case checks whether a Sub-Category field is similarly null. If true, then the query labels the field All Sub-Categories.

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';
CategorySub-Categorysum_sales
FurnitureBookcases114,348
 Chairs328,237
 Furnishings91,514
 Tables206,966
 -92
 All Sub-Categories741,156
Office SuppliesAppliances107,532
 Art27,119
 Binders203,413
 Envelopes16,363
 Fasteners3,024
 Labels12,486
 Paper78,479
 Storage223,844
 Supplies46,674
 -273
 All Sub-Categories719,206
TechnologyAccessories167,380
 Copiers149,528
 Machines189,239
 Phones329,636
 -259
 All Sub-Categories836,041
-Computers113
 Projectors744
 -562
 All Sub-Categories1,420
All CategoriesAll Sub-Categories2,297,824

See Also