Senior
Aggregation
SQL & Databases
Write a query using GROUPING SETS / ROLLUP.
Short answer: ROLLUP creates subtotals and a grand total. GROUPING SETS lists exact aggregation levels. GROUPING(col) detects total rows (NULLs that mean “all”).
Sample solution
T-SQL
SELECT DepartmentId, JobTitle, SUM(Salary) AS TotalSalary FROM Employees GROUP BY ROLLUP (DepartmentId, JobTitle);
Use GROUPING() to distinguish real NULL dimension values from rollup NULLs.
Share this Q&A
Share preview image: https://www.toolliyo.com/images/toolliyo-logo.png