Create Sample SQL Server Database and Data: GROUING SETS. These Options Are Similar, But Produce Different Results
Create Sample SQL Server Database and Data: GROUING SETS. These Options Are Similar, But Produce Different Results
USE EmpTest
GO
Below are the results. The first query returns 2 rows by Department with Salary
totals, since there are 2 departments. The second query returns 6 rows by
Department and Category with Salary totals, since there are 2 departments with 3
categories in each department.
Below are the only 2 rows that meet the criteria. We can double check this by
looking at the query results from the first set of queries above.
Advertisement
The first query results show the 2 Departments and the total, but also the grand
total for these 2 Departments. The second query results show us all of the
combinations of Department and Category. For example, we see IT (department)
and A (category) and 16000 (total), then Sales (department) and A (category) and
7000 (total) and then NULL (both departments) and A (category) and 15000
(total). In the chart, I break down the different groupings that are part of this
second query.
We can see the first query results are the same as Group By Rollup example, but
the second query only returns 9 rows instead of 12 that we got in the Group By
Rollup query. The second query does the rollup first by Department and then by
Category, which is different from the Group By Cube which did the rollup in both
directions. This allows us to get subtotals for each Department and an overall total
for all Departments.
We could change the second query, as shown below, to first rollup by Category
and then Department.
SELECT
Department,
SUM(Salary) as salary
FROM EmpSalary
GROUP BY ROLLUP(Department)
SELECT
Department,
Category,
SUM(Salary) as salary
FROM EmpSalary
GROUP BY ROLLUP (Category, Department)
We can see the results for the second query now do the grouping based on
Category and then Department. And we still get the subtotals and totals.
Advertisement
SQL Server GROUP BY ROLLUP with GROUPING_ID
Example
Another option is to use GROUPING_ID as part of the result set to show each
group.
SELECT
Department,
Category,
SUM(Salary) as Salary,
GROUPING_ID(Category, Department) as GroupingID
FROM EmpSalary
GROUP BY ROLLUP(Category, Department)
We can see we have the same results as above, but now we have a grouping
value for each of these groups.
SQL Server GROUP BY GROUPING SETS Example
With grouping sets, we can determine how we want the data to be put together.
SELECT
Department,
Category,
SUM(Salary) as Salary
FROM EmpSalary
GROUP BY GROUPING SETS(Category, Department,(Category, Department),())
Below we can see we did a group for Category, another group for Department,
another group for Category and Department and the last group for NULL.
Here is another example by Department and Category and an overall group for
NULL.
SELECT
Department,
Category,
SUM(Salary) as Salary
FROM EmpSalary
GROUP BY GROUPING SETS((Department, Category),())
We could also take this a step further and use CUBE and ROLLUP for the different
grouping sets.
SELECT
Department,
Category,
SUM(Salary) as Salary
FROM EmpSalary
GROUP BY GROUPING SETS(CUBE(Department, Category), ROLLUP(Department,
Category))