GROUPING()

Use the GROUPING() clause with ROLLUP to determine whether null values are the result of a ROLLUP operation or part of your dataset. GROUPING() returns 1 if the null value is a subtotal generated by a rollup. It returns 0 otherwise.

This example uses the GROUPING() function and CASE statements together to label the subtotal and grand total categories. 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.” Similarly, the second case checks whether a Sub-Category field is null. If true, the query labels the field “All Sub-Categories.”

CategorySub-Categorysum_sales
FurnitureBookcases114,880
 Chairs328,449.1
 Furnishings91,705.16
 Tables206,965.53
 All Sub-Categories741,999.8
Office SuppliesAppliances107,532.16
 Art27,118.73
 Binders203,412.73
 Envelopes16,476.4
 Fasteners3,024.28
 Labels12,486.31
 Paper78,479.21
 Storage223,843.61
 Supplies46,673.544
 All Sub-Categories719,047.03
TechnologyAccessories167,380.32
 Copiers149,528.03
 Machines189,238.63
 Phones330,007.05
 All Sub-Categories838,154.03
All CategoriesAll Sub-Categories2,297,200.86