T-SQL Tutorial

GROUPING SETS clause


In SQL Server, the GROUPING SETS clause is used to group data based on multiple columns, and generate aggregated results for each unique combination of values in those columns. The GROUPING SETS clause is a powerful tool for generating complex summary reports and business intelligence dashboards.


Syntax

The syntax for using GROUPING SETS is as follows:

SELECT column1, column2, ..., aggregate_function(column_name)
FROM table_name
GROUP BY GROUPING SETS (
(column1, column2),
(column1),
(column2),
()
);

In this syntax, the GROUP BY clause includes a list of grouping sets, each of which is enclosed in parentheses. The first grouping set includes two columns (column1 and column2), the second grouping set includes only column1, the third grouping set includes only column2, and the fourth grouping set includes no columns (which corresponds to the grand total).


The GROUPING SETS clause allows you to specify multiple grouping sets within a single SELECT statement. Each grouping set is defined as a list of columns that you want to group by. For example, if you have a table of sales data with columns for date, region, and product, you might want to generate summary data for each region, each product, and for each region and product combination. With GROUPING SETS, you can specify all of these grouping sets in a single statement.


Example

Here's an example of how to use the GROUPING SETS clause:

SELECT region, product, SUM(sales) AS total_sales
FROM sales_table
GROUP BY GROUPING SETS ((region), (product), (region, product))


In this example, we're grouping by region, product, and region and product combined. The result set will include subtotals for each region, each product, and for each region and product combination.

The GROUPING SETS clause also allows you to use the GROUPING function to determine whether a particular column is being used in the current grouping set. The GROUPING function returns 1 if the column is not in the current grouping set, and 0 if it is. This can be useful for generating custom summary labels and formatting.

For example, you might want to display a custom label for each subtotal row that indicates which columns are being used for the current grouping set. You can do this with a CASE statement that uses the GROUPING function:

SELECT
CASE
WHEN GROUPING(region) = 1 AND GROUPING(product) = 1 THEN 'Total'
WHEN GROUPING(region) = 0 AND GROUPING(product) = 1 THEN region + ' Total'
WHEN GROUPING(region) = 1 AND GROUPING(product) = 0 THEN product + ' Total'
ELSE region + ' - ' + product
END AS group_label,
SUM(sales) AS total_sales
FROM sales_table
GROUP BY GROUPING SETS ((region), (product), (region, product))

In this example, we're using a CASE statement to generate custom labels for each subtotal row. If both region and product are not in the current grouping set, we label the row as "Total". If only product is not in the grouping set, we label the row with the region name and "Total". If only region is not in the grouping set, we label the row with the product name and "Total". Otherwise, we label the row with the region and product names concatenated with a hyphen.

In summary, the GROUPING SETS clause in SQL Server is a powerful tool for generating complex summary reports and business intelligence dashboards. It allows you to group data based on multiple columns and generate subtotals for each unique combination of values in those columns. You can also use the GROUPING function to determine which columns are in the current grouping set and generate custom summary labels and formatting.