Understanding SQL ROLLUP and CUBE
SQL Server allows us to use ROLLUP and CUBE for hierarchical and multidimensional aggregations.
ROLLUP:
- Generates subtotals and a grand total for a hierarchy of columns.
- Useful for generating reports with hierarchical data.
- Can be used with GROUP BY to create subtotals.
- Example: If grouping by Year, Month, and Day, ROLLUP will produce:
- Year, Month, Day
- Year, Month
- Year
- Grand Total
CUBE:
- Generates subtotals for all possible combinations of columns.
- Useful for multidimensional analysis.
- Can be used with GROUP BY to create subtotals for all combinations.
- Example: If grouping by Year, Month, and Day, CUBE will produce:
- Year, Month, Day
- Year, Month
- Year, Day
- Year
- Month, Day
- Month
- Day
- Grand Total
Sample Table and Data
CREATE TABLE Sales (
ID INT,
FName VARCHAR(30),
Zone VARCHAR(30),
Sale INT
);
INSERT INTO Sales (ID, FName, Zone, Sale)
VALUES
(1, 'Mangal', 'East', 20),
(2, 'Mangal', 'East', 150),
(3, 'Mangal', 'West', 50),
(4, 'Ram', 'East', 45),
(5, 'Ram', NULL, 80),
(6, 'Ram', NULL, 40),
(7, 'Sachin', 'West', 50),
(8, 'Sachin', 'West', 40);
Sample Data:
| ID | FName | Zone | Sale |
|---|---|---|---|
| 1 | Mangal | East | 20 |
| 2 | Mangal | East | 150 |
| 3 | Mangal | West | 50 |
| 4 | Ram | East | 45 |
| 5 | Ram | Unknown | 80 |
| 6 | Ram | Unknown | 40 |
| 7 | Sachin | West | 50 |
| 8 | Sachin | West | 40 |
SQL ROLLUP Query
SELECT
CASE GROUPING(FName)
WHEN 1 THEN 'All Names'
ELSE ISNULL(FName, 'Unknown')
END AS FName,
CASE GROUPING(Zone)
WHEN 1 THEN 'All Zones'
ELSE ISNULL(Zone, 'Unknown')
END AS Zone,
SUM(Sale) AS Total
FROM Sales
GROUP BY FName, Zone WITH ROLLUP
ORDER BY GROUPING(FName), FName, GROUPING(Zone), Zone;
ROLLUP Output:
| FName | Zone | Total |
|---|---|---|
| Mangal | East | 170 |
| Mangal | West | 50 |
| Mangal | All Zones | 220 |
| Ram | East | 45 |
| Ram | Unknown | 120 |
| Ram | All Zones | 165 |
| Sachin | West | 90 |
| Sachin | All Zones | 90 |
| All Names | All Zones | 475 |
Explanation:
The ROLLUP query generates subtotals for each name and zone, as well as a grand total for all names and zones.
SQL CUBE Query
SELECT
CASE GROUPING(FName)
WHEN 1 THEN 'All Names'
ELSE ISNULL(FName, 'Unknown')
END AS FName,
CASE GROUPING(Zone)
WHEN 1 THEN 'All Zones'
ELSE ISNULL(Zone, 'Unknown')
END AS Zone,
SUM(Sale) AS Total
FROM Sales
GROUP BY FName, Zone WITH CUBE
ORDER BY GROUPING(FName), FName, GROUPING(Zone), Zone;
CUBE Output:
| FName | Zone | Total |
|---|---|---|
| Mangal | East | 170 |
| Mangal | West | 50 |
| Mangal | All Zones | 220 |
| Ram | East | 45 |
| Ram | Unknown | 120 |
| Ram | All Zones | 165 |
| Sachin | West | 90 |
| Sachin | All Zones | 90 |
| All Names | East | 215 |
| All Names | West | 140 |
| All Names | Unknown | 120 |
| All Names | All Zones | 475 |
Explanation:
The CUBE query generates subtotals for all combinations of names and zones, as well as a grand total for all names and zones.
Compute:
Compute is a SQL Server feature that allows you to calculate running totals and other aggregations on the result set. It can be used with ROLLUP and CUBE to provide additional insights into your data.
SQL CUBE Query
SELECT ProductID, OrderID, Quantity
FROM [Order Details]
ORDER BY ProductID, OrderID
COMPUTE SUM(Quantity);
Conclusion
ROLLUP is used for hierarchical aggregations, while CUBE is used for multidimensional aggregations. Choose the one that best fits your reporting needs.