The CUBE and ROLLUP Operators
To facilitate the use of summary information in client applications, SQL Server 2000 provides the ROLLUP and CUBE operators.
As you can see in Listing 11.20, using ROLLUP you can get aggregations at different levels in a single result set. Remember that using COMPUTE, you obtain a reportlike result set (see Listing 11.19).
Code Listing 11.20. Use ROLLUP to Produce a Result Set with Aggregations at Different Levels
SELECT CategoryName AS Category, ProductName AS Product, SUM((OD.UnitPrice * Quantity * (1-Discount))) AS Price, AVG(Discount) AS AvgDiscount FROM [Order Details] OD JOIN Orders O ON O.OrderID = OD.OrderID ... |
Get Microsoft® SQL Server™ 2000 Programming by Example now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.