Aggregation WITH CUBE

Published Mar 23 2019 05:08 AM 178 Views
Microsoft
First published on MSDN on Sep 27, 2007

In my last post, I wrote about how aggregation WITH ROLLUP works.  In this post, I will discuss how aggregation WITH CUBE works.  Like the WITH ROLLUP clause, the WITH CUBE clause permits us to compute multiple "levels" of aggregation in a single statement.  To understand the difference between these two clauses, let's look at an example.  We'll use the same fictitious sales data from last week's example.



CREATE TABLE Sales (EmpId INT, Yr INT, Sales MONEY)
INSERT Sales VALUES(1, 2005, 12000)
INSERT Sales VALUES(1, 2006, 18000)
INSERT Sales VALUES(1, 2007, 25000)
INSERT Sales VALUES(2, 2005, 15000)
INSERT Sales VALUES(2, 2006, 6000)
INSERT Sales VALUES(3, 2006, 20000)
INSERT Sales VALUES(3, 2007, 24000)


Consider the following query from last week:



SELECT EmpId, Yr, SUM(Sales) AS Sales
FROM Sales
GROUP BY EmpId, Yr WITH ROLLUP


It will be easier to see what is happening if we pivot the sales data:




Yr


2005


2006


2007


ALL


EmpId


1


12000.00


18000.00


25000.00


55000.00


2


15000.00


6000.00



21000.00


3



20000.00


24000.00


44000.00


ALL





120000.00


The table clearly shows that the WITH ROLLUP clause computes the total for each employee for all years and the grand total for all employees and all years.  The query does not compute the totals for each year for all employees.  Moreover, the order of the columns in the GROUP BY clause determines in which order the data is totaled.


Now let's repeat the same query but replace the WITH ROLLUP clause with a WITH CUBE clause:



SELECT EmpId, Yr, SUM(Sales) AS Sales
FROM Sales
GROUP BY EmpId, Yr WITH CUBE


This query computes all possible sub-totals and totals:




Yr


2005


2006


2007


ALL


EmpId


1


12000.00


18000.00


25000.00


55000.00


2


15000.00


6000.00



21000.00


3



20000.00


24000.00


44000.00


ALL


27000.00


44000.00


49000.00


120000.00


Because the WITH CUBE clause causes the query to compute all possible totals, the order of the columns in the GROUP BY clause does not matter.  Of course, by default, SQL Server does not pivot the results of either of the above queries.  Here is the actual output from the WITH CUBE query:

EmpId Yr Sales
----------- ----------- ---------------------
1 2005 12000.00
1 2006 18000.00
1 2007 25000.00
1 NULL 55000.00
2 2005 15000.00
2 2006 6000.00
2 NULL 21000.00
3 2006 20000.00
3 2007 24000.00
3 NULL 44000.00
NULL NULL 120000.00
NULL 2005 27000.00
NULL 2006 44000.00
NULL 2007 49000.00

Next, let's look at the query plan for the WITH CUBE query:


|--Compute Scalar(DEFINE:([Expr1004]=CASE WHEN [Expr1005]=(0) THEN NULL ELSE [Expr1006] END))
|--Concatenation
|--Stream Aggregate(GROUP BY:([Sales].[EmpId], [Sales].[Yr]) DEFINE:([Expr1005]=SUM([Expr1007]), [Expr1006]=SUM([Expr1008])))
|    |--Sort(ORDER BY:([Sales].[EmpId] ASC, [Sales].[Yr] ASC))
|         |--Table Spool
|              |--Stream Aggregate(GROUP BY:([Sales].[Yr], [Sales].[EmpId]) DEFINE:([Expr1007]=COUNT_BIG([Sales].[Sales]), [Expr1008]=SUM([Sales].[Sales])))
|                   |--Sort(ORDER BY:([Sales].[Yr] ASC, [Sales].[EmpId] ASC))
|                        |--Table Scan(OBJECT:([Sales]))
|--Compute Scalar(DEFINE:([Expr1012]=NULL))
|--Stream Aggregate(GROUP BY:([Sales].[Yr]) DEFINE:([Expr1005]=SUM([Expr1007]), [Expr1006]=SUM([Expr1008])))
|--Table Spool


This plan consists of two parts.  SQL Server has effectively rewritten our query as follows:



SELECT EmpId, Yr, SUM(Sales) AS Sales
FROM Sales
GROUP BY EmpId, Yr WITH ROLLUP
UNION ALL
SELECT NULL, Yr, SUM(Sales)
FROM Sales
GROUP BY Yr


The first part of the plan computes the result for the WITH ROLLUP query above.  I described how this query plan works in last week's post.  The second part of this plan computes the missing year sub-totals yielding the entire CUBE result.  Note that this plan employs a common sub-expression spool.  As I discussed in this post , a common sub-expression spool copies its input rows into a worktable and then reads and returns the rows from the worktable multiple times - in this case twice.  The spool is meant to improve performance although, in this example, it has little impact since the server could just as easily have re-read the original Sales table.  However, if the input to the aggregation was more complex and cost more to evaluate, the spool would help.


If we use the WITH CUBE clause when aggregating on more than two columns, SQL Server simply generates increasingly complex plans with additional inputs to the concatentation operator.  As with the simple two column example, the idea is to compute the whole CUBE by computing all of the individual ROLLUPs that compose it.


Finally, we can actually combine WITH CUBE and PIVOT to generate the above table in a single simple statement.  (I actually proposed a variation of this query in an answer to a reader's comment on my post about the PIVOT operator but I like this solution better.)



SELECT EmpId, [2005], [2006], [2007], [ALL]
FROM
(
SELECT
CASE WHEN GROUPING(EmpId) = 0
THEN CAST (EmpId AS CHAR(7))
ELSE 'ALL'
END AS EmpId,
CASE WHEN GROUPING(Yr) = 0
THEN CAST (Yr AS CHAR(7))
ELSE 'ALL'
END AS Yr,
SUM(Sales) AS Sales
FROM Sales
GROUP BY EmpId, Yr WITH CUBE
) AS s
PIVOT (SUM(Sales) FOR Yr IN ([2005], [2006], [2007], [ALL])) AS p


Here is the output from this query:

EmpId 2005 2006 2007 ALL
------- --------------------- --------------------- --------------------- ---------------------
1 12000.00 18000.00 25000.00 55000.00
2 15000.00 6000.00 NULL 21000.00
3 NULL 20000.00 24000.00 44000.00
ALL 27000.00 44000.00 49000.00 120000.00
%3CLINGO-SUB%20id%3D%22lingo-sub-383320%22%20slang%3D%22en-US%22%3EAggregation%20WITH%20CUBE%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-383320%22%20slang%3D%22en-US%22%3E%0A%20%26lt%3Bmeta%20http-equiv%3D%22Content-Type%22%20content%3D%22text%2Fhtml%3B%20charset%3DUTF-8%22%20%2F%26gt%3B%3CSTRONG%3E%20First%20published%20on%20MSDN%20on%20Sep%2027%2C%202007%20%3C%2FSTRONG%3E%20%3CBR%20%2F%3E%3CP%3EIn%20my%20last%20post%2C%20I%20wrote%20about%20how%20%3CA%20href%3D%22http%3A%2F%2Fblogs.msdn.com%2Fcraigfr%2Farchive%2F2007%2F09%2F21%2Faggregation-with-rollup.aspx%22%20mce_href%3D%22http%3A%2F%2Fblogs.msdn.com%2Fcraigfr%2Farchive%2F2007%2F09%2F21%2Faggregation-with-rollup.aspx%22%20title%3D%22Aggregation%20WITH%20ROLLUP%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3E%20aggregation%20WITH%20ROLLUP%20%3C%2FA%3E%20works.%26nbsp%3B%20In%20this%20post%2C%20I%20will%20discuss%20how%20aggregation%20WITH%20CUBE%20works.%26nbsp%3B%20Like%20the%20WITH%20ROLLUP%20clause%2C%20the%20WITH%20CUBE%20clause%20permits%20us%20to%20compute%20multiple%20%22levels%22%20of%20aggregation%20in%20a%20single%20statement.%26nbsp%3B%20To%20understand%20the%20difference%20between%20these%20two%20clauses%2C%20let's%20look%20at%20an%20example.%26nbsp%3B%20We'll%20use%20the%20same%20fictitious%20sales%20data%20from%20last%20week's%20example.%3C%2FP%3E%3CBR%20%2F%3E%3CBLOCKQUOTE%3E%3CBR%20%2F%3E%3CP%3ECREATE%20TABLE%20Sales%20(EmpId%20INT%2C%20Yr%20INT%2C%20Sales%20MONEY)%20%3CBR%20%2F%3E%20INSERT%20Sales%20VALUES(1%2C%202005%2C%2012000)%20%3CBR%20%2F%3E%20INSERT%20Sales%20VALUES(1%2C%202006%2C%2018000)%20%3CBR%20%2F%3E%20INSERT%20Sales%20VALUES(1%2C%202007%2C%2025000)%20%3CBR%20%2F%3E%20INSERT%20Sales%20VALUES(2%2C%202005%2C%2015000)%20%3CBR%20%2F%3E%20INSERT%20Sales%20VALUES(2%2C%202006%2C%206000)%20%3CBR%20%2F%3E%20INSERT%20Sales%20VALUES(3%2C%202006%2C%2020000)%20%3CBR%20%2F%3E%20INSERT%20Sales%20VALUES(3%2C%202007%2C%2024000)%3C%2FP%3E%0A%20%20%3C%2FBLOCKQUOTE%3E%3CBR%20%2F%3E%3CP%3EConsider%20the%20following%20query%20from%20last%20week%3A%3C%2FP%3E%3CBR%20%2F%3E%3CBLOCKQUOTE%3E%3CBR%20%2F%3E%3CP%3ESELECT%20EmpId%2C%20Yr%2C%20SUM(Sales)%20AS%20Sales%20%3CBR%20%2F%3E%20FROM%20Sales%20%3CBR%20%2F%3E%20GROUP%20BY%20EmpId%2C%20Yr%20WITH%20ROLLUP%3C%2FP%3E%0A%20%20%3C%2FBLOCKQUOTE%3E%3CBR%20%2F%3E%3CP%3EIt%20will%20be%20easier%20to%20see%20what%20is%20happening%20if%20we%20pivot%20the%20sales%20data%3A%3C%2FP%3E%3CBR%20%2F%3E%3CTABLE%3E%0A%20%20%20%3CTBODY%3E%3CTR%3E%0A%20%20%20%20%3CTD%3E%3CBR%20%2F%3E%3CP%3E%3C%2FP%3E%0A%20%20%20%20%3C%2FTD%3E%0A%20%20%20%20%3CTD%3E%3CBR%20%2F%3E%3CP%3E%3CB%3E%20Yr%20%3C%2FB%3E%3C%2FP%3E%0A%20%20%20%20%3C%2FTD%3E%0A%20%20%20%3C%2FTR%3E%0A%20%20%20%3CTR%3E%0A%20%20%20%20%3CTD%3E%3CBR%20%2F%3E%3CP%3E%3CB%3E%202005%20%3C%2FB%3E%3C%2FP%3E%0A%20%20%20%20%3C%2FTD%3E%0A%20%20%20%20%3CTD%3E%3CBR%20%2F%3E%3CP%3E%3CB%3E%202006%20%3C%2FB%3E%3C%2FP%3E%0A%20%20%20%20%3C%2FTD%3E%0A%20%20%20%20%3CTD%3E%3CBR%20%2F%3E%3CP%3E%3CB%3E%202007%20%3C%2FB%3E%3C%2FP%3E%0A%20%20%20%20%3C%2FTD%3E%0A%20%20%20%20%3CTD%3E%3CBR%20%2F%3E%3CP%3E%3CB%3E%20ALL%20%3C%2FB%3E%3C%2FP%3E%0A%20%20%20%20%3C%2FTD%3E%0A%20%20%20%3C%2FTR%3E%0A%20%20%20%3CTR%3E%0A%20%20%20%20%3CTD%3E%3CBR%20%2F%3E%3CP%3E%3CB%3E%20EmpId%20%3C%2FB%3E%3C%2FP%3E%0A%20%20%20%20%3C%2FTD%3E%0A%20%20%20%20%3CTD%3E%3CBR%20%2F%3E%3CP%3E%3CB%3E%201%20%3C%2FB%3E%3C%2FP%3E%0A%20%20%20%20%3C%2FTD%3E%0A%20%20%20%20%3CTD%3E%3CBR%20%2F%3E%3CP%3E12000.00%3C%2FP%3E%0A%20%20%20%20%3C%2FTD%3E%0A%20%20%20%20%3CTD%3E%3CBR%20%2F%3E%3CP%3E18000.00%3C%2FP%3E%0A%20%20%20%20%3C%2FTD%3E%0A%20%20%20%20%3CTD%3E%3CBR%20%2F%3E%3CP%3E25000.00%3C%2FP%3E%0A%20%20%20%20%3C%2FTD%3E%0A%20%20%20%20%3CTD%3E%3CBR%20%2F%3E%3CP%3E%3CB%3E%2055000.00%20%3C%2FB%3E%3C%2FP%3E%0A%20%20%20%20%3C%2FTD%3E%0A%20%20%20%3C%2FTR%3E%0A%20%20%20%3CTR%3E%0A%20%20%20%20%3CTD%3E%3CBR%20%2F%3E%3CP%3E%3CB%3E%202%20%3C%2FB%3E%3C%2FP%3E%0A%20%20%20%20%3C%2FTD%3E%0A%20%20%20%20%3CTD%3E%3CBR%20%2F%3E%3CP%3E15000.00%3C%2FP%3E%0A%20%20%20%20%3C%2FTD%3E%0A%20%20%20%20%3CTD%3E%3CBR%20%2F%3E%3CP%3E6000.00%3C%2FP%3E%0A%20%20%20%20%3C%2FTD%3E%0A%20%20%20%20%3CTD%3E%3CBR%20%2F%3E%3CP%3E%3C%2FP%3E%0A%20%20%20%20%3C%2FTD%3E%0A%20%20%20%20%3CTD%3E%3CBR%20%2F%3E%3CP%3E%3CB%3E%2021000.00%20%3C%2FB%3E%3C%2FP%3E%0A%20%20%20%20%3C%2FTD%3E%0A%20%20%20%3C%2FTR%3E%0A%20%20%20%3CTR%3E%0A%20%20%20%20%3CTD%3E%3CBR%20%2F%3E%3CP%3E%3CB%3E%203%20%3C%2FB%3E%3C%2FP%3E%0A%20%20%20%20%3C%2FTD%3E%0A%20%20%20%20%3CTD%3E%3CBR%20%2F%3E%3CP%3E%3C%2FP%3E%0A%20%20%20%20%3C%2FTD%3E%0A%20%20%20%20%3CTD%3E%3CBR%20%2F%3E%3CP%3E20000.00%3C%2FP%3E%0A%20%20%20%20%3C%2FTD%3E%0A%20%20%20%20%3CTD%3E%3CBR%20%2F%3E%3CP%3E24000.00%3C%2FP%3E%0A%20%20%20%20%3C%2FTD%3E%0A%20%20%20%20%3CTD%3E%3CBR%20%2F%3E%3CP%3E%3CB%3E%2044000.00%20%3C%2FB%3E%3C%2FP%3E%0A%20%20%20%20%3C%2FTD%3E%0A%20%20%20%3C%2FTR%3E%0A%20%20%20%3CTR%3E%0A%20%20%20%20%3CTD%3E%3CBR%20%2F%3E%3CP%3E%3CB%3E%20ALL%20%3C%2FB%3E%3C%2FP%3E%0A%20%20%20%20%3C%2FTD%3E%0A%20%20%20%20%3CTD%3E%3CBR%20%2F%3E%3CP%3E%3CB%3E%20%3C%2FB%3E%3C%2FP%3E%0A%20%20%20%20%3C%2FTD%3E%0A%20%20%20%20%3CTD%3E%3CBR%20%2F%3E%3CP%3E%3CB%3E%20%3C%2FB%3E%3C%2FP%3E%0A%20%20%20%20%3C%2FTD%3E%0A%20%20%20%20%3CTD%3E%3CBR%20%2F%3E%3CP%3E%3CB%3E%20%3C%2FB%3E%3C%2FP%3E%0A%20%20%20%20%3C%2FTD%3E%0A%20%20%20%20%3CTD%3E%3CBR%20%2F%3E%3CP%3E%3CB%3E%20120000.00%20%3C%2FB%3E%3C%2FP%3E%0A%20%20%20%20%3C%2FTD%3E%0A%20%20%20%3C%2FTR%3E%0A%20%20%3C%2FTBODY%3E%3C%2FTABLE%3E%3CBR%20%2F%3E%3CP%3EThe%20table%20clearly%20shows%20that%20the%20WITH%20ROLLUP%20clause%20computes%20the%20total%20for%20each%20employee%20for%20all%20years%20and%20the%20grand%20total%20for%20all%20employees%20and%20all%20years.%26nbsp%3B%20The%20query%20does%20not%20compute%20the%20totals%20for%20each%20year%20for%20all%20employees.%26nbsp%3B%20Moreover%2C%20the%20order%20of%20the%20columns%20in%20the%20GROUP%20BY%20clause%20determines%20in%20which%20order%20the%20data%20is%20totaled.%3C%2FP%3E%3CBR%20%2F%3E%3CP%3ENow%20let's%20repeat%20the%20same%20query%20but%20replace%20the%20WITH%20ROLLUP%20clause%20with%20a%20WITH%20CUBE%20clause%3A%3C%2FP%3E%3CBR%20%2F%3E%3CBLOCKQUOTE%3E%3CBR%20%2F%3E%3CP%3ESELECT%20EmpId%2C%20Yr%2C%20SUM(Sales)%20AS%20Sales%20%3CBR%20%2F%3E%20FROM%20Sales%20%3CBR%20%2F%3E%20GROUP%20BY%20EmpId%2C%20Yr%20WITH%20CUBE%3C%2FP%3E%0A%20%20%3C%2FBLOCKQUOTE%3E%3CBR%20%2F%3E%3CP%3EThis%20query%20computes%20all%20possible%20sub-totals%20and%20totals%3A%3C%2FP%3E%3CBR%20%2F%3E%3CTABLE%3E%0A%20%20%20%3CTBODY%3E%3CTR%3E%0A%20%20%20%20%3CTD%3E%3CBR%20%2F%3E%3CP%3E%3C%2FP%3E%0A%20%20%20%20%3C%2FTD%3E%0A%20%20%20%20%3CTD%3E%3CBR%20%2F%3E%3CP%3E%3CB%3E%20Yr%20%3C%2FB%3E%3C%2FP%3E%0A%20%20%20%20%3C%2FTD%3E%0A%20%20%20%3C%2FTR%3E%0A%20%20%20%3CTR%3E%0A%20%20%20%20%3CTD%3E%3CBR%20%2F%3E%3CP%3E%3CB%3E%202005%20%3C%2FB%3E%3C%2FP%3E%0A%20%20%20%20%3C%2FTD%3E%0A%20%20%20%20%3CTD%3E%3CBR%20%2F%3E%3CP%3E%3CB%3E%202006%20%3C%2FB%3E%3C%2FP%3E%0A%20%20%20%20%3C%2FTD%3E%0A%20%20%20%20%3CTD%3E%3CBR%20%2F%3E%3CP%3E%3CB%3E%202007%20%3C%2FB%3E%3C%2FP%3E%0A%20%20%20%20%3C%2FTD%3E%0A%20%20%20%20%3CTD%3E%3CBR%20%2F%3E%3CP%3E%3CB%3E%20ALL%20%3C%2FB%3E%3C%2FP%3E%0A%20%20%20%20%3C%2FTD%3E%0A%20%20%20%3C%2FTR%3E%0A%20%20%20%3CTR%3E%0A%20%20%20%20%3CTD%3E%3CBR%20%2F%3E%3CP%3E%3CB%3E%20EmpId%20%3C%2FB%3E%3C%2FP%3E%0A%20%20%20%20%3C%2FTD%3E%0A%20%20%20%20%3CTD%3E%3CBR%20%2F%3E%3CP%3E%3CB%3E%201%20%3C%2FB%3E%3C%2FP%3E%0A%20%20%20%20%3C%2FTD%3E%0A%20%20%20%20%3CTD%3E%3CBR%20%2F%3E%3CP%3E12000.00%3C%2FP%3E%0A%20%20%20%20%3C%2FTD%3E%0A%20%20%20%20%3CTD%3E%3CBR%20%2F%3E%3CP%3E18000.00%3C%2FP%3E%0A%20%20%20%20%3C%2FTD%3E%0A%20%20%20%20%3CTD%3E%3CBR%20%2F%3E%3CP%3E25000.00%3C%2FP%3E%0A%20%20%20%20%3C%2FTD%3E%0A%20%20%20%20%3CTD%3E%3CBR%20%2F%3E%3CP%3E%3CB%3E%2055000.00%20%3C%2FB%3E%3C%2FP%3E%0A%20%20%20%20%3C%2FTD%3E%0A%20%20%20%3C%2FTR%3E%0A%20%20%20%3CTR%3E%0A%20%20%20%20%3CTD%3E%3CBR%20%2F%3E%3CP%3E%3CB%3E%202%20%3C%2FB%3E%3C%2FP%3E%0A%20%20%20%20%3C%2FTD%3E%0A%20%20%20%20%3CTD%3E%3CBR%20%2F%3E%3CP%3E15000.00%3C%2FP%3E%0A%20%20%20%20%3C%2FTD%3E%0A%20%20%20%20%3CTD%3E%3CBR%20%2F%3E%3CP%3E6000.00%3C%2FP%3E%0A%20%20%20%20%3C%2FTD%3E%0A%20%20%20%20%3CTD%3E%3CBR%20%2F%3E%3CP%3E%3C%2FP%3E%0A%20%20%20%20%3C%2FTD%3E%0A%20%20%20%20%3CTD%3E%3CBR%20%2F%3E%3CP%3E%3CB%3E%2021000.00%20%3C%2FB%3E%3C%2FP%3E%0A%20%20%20%20%3C%2FTD%3E%0A%20%20%20%3C%2FTR%3E%0A%20%20%20%3CTR%3E%0A%20%20%20%20%3CTD%3E%3CBR%20%2F%3E%3CP%3E%3CB%3E%203%20%3C%2FB%3E%3C%2FP%3E%0A%20%20%20%20%3C%2FTD%3E%0A%20%20%20%20%3CTD%3E%3CBR%20%2F%3E%3CP%3E%3C%2FP%3E%0A%20%20%20%20%3C%2FTD%3E%0A%20%20%20%20%3CTD%3E%3CBR%20%2F%3E%3CP%3E20000.00%3C%2FP%3E%0A%20%20%20%20%3C%2FTD%3E%0A%20%20%20%20%3CTD%3E%3CBR%20%2F%3E%3CP%3E24000.00%3C%2FP%3E%0A%20%20%20%20%3C%2FTD%3E%0A%20%20%20%20%3CTD%3E%3CBR%20%2F%3E%3CP%3E%3CB%3E%2044000.00%20%3C%2FB%3E%3C%2FP%3E%0A%20%20%20%20%3C%2FTD%3E%0A%20%20%20%3C%2FTR%3E%0A%20%20%20%3CTR%3E%0A%20%20%20%20%3CTD%3E%3CBR%20%2F%3E%3CP%3E%3CB%3E%20ALL%20%3C%2FB%3E%3C%2FP%3E%0A%20%20%20%20%3C%2FTD%3E%0A%20%20%20%20%3CTD%3E%3CBR%20%2F%3E%3CP%3E%3CB%3E%20%3CI%3E%2027000.00%20%3C%2FI%3E%20%3C%2FB%3E%3C%2FP%3E%0A%20%20%20%20%3C%2FTD%3E%0A%20%20%20%20%3CTD%3E%3CBR%20%2F%3E%3CP%3E%3CB%3E%20%3CI%3E%2044000.00%20%3C%2FI%3E%20%3C%2FB%3E%3C%2FP%3E%0A%20%20%20%20%3C%2FTD%3E%0A%20%20%20%20%3CTD%3E%3CBR%20%2F%3E%3CP%3E%3CB%3E%20%3CI%3E%2049000.00%20%3C%2FI%3E%20%3C%2FB%3E%3C%2FP%3E%0A%20%20%20%20%3C%2FTD%3E%0A%20%20%20%20%3CTD%3E%3CBR%20%2F%3E%3CP%3E%3CB%3E%20120000.00%20%3C%2FB%3E%3C%2FP%3E%0A%20%20%20%20%3C%2FTD%3E%0A%20%20%20%3C%2FTR%3E%0A%20%20%3C%2FTBODY%3E%3C%2FTABLE%3E%3CBR%20%2F%3E%3CP%3EBecause%20the%20WITH%20CUBE%20clause%20causes%20the%20query%20to%20compute%20all%20possible%20totals%2C%20the%20order%20of%20the%20columns%20in%20the%20GROUP%20BY%20clause%20does%20not%20matter.%26nbsp%3B%20Of%20course%2C%20by%20default%2C%20SQL%20Server%20does%20not%20pivot%20the%20results%20of%20either%20of%20the%20above%20queries.%26nbsp%3B%20Here%20is%20the%20actual%20output%20from%20the%20WITH%20CUBE%20query%3A%3C%2FP%3EEmpId%20Yr%20Sales%20%3CBR%20%2F%3E%20-----------%20-----------%20---------------------%20%3CBR%20%2F%3E%201%202005%2012000.00%20%3CBR%20%2F%3E%201%202006%2018000.00%20%3CBR%20%2F%3E%201%202007%2025000.00%20%3CBR%20%2F%3E%201%20NULL%2055000.00%20%3CBR%20%2F%3E%202%202005%2015000.00%20%3CBR%20%2F%3E%202%202006%206000.00%20%3CBR%20%2F%3E%202%20NULL%2021000.00%20%3CBR%20%2F%3E%203%202006%2020000.00%20%3CBR%20%2F%3E%203%202007%2024000.00%20%3CBR%20%2F%3E%203%20NULL%2044000.00%20%3CBR%20%2F%3E%20NULL%20NULL%20120000.00%20%3CBR%20%2F%3E%20NULL%202005%2027000.00%20%3CBR%20%2F%3E%20NULL%202006%2044000.00%20%3CBR%20%2F%3E%20NULL%202007%2049000.00%20%3CBR%20%2F%3E%3CP%3ENext%2C%20let's%20look%20at%20the%20query%20plan%20for%20the%20WITH%20CUBE%20query%3A%3C%2FP%3E%3CBR%20%2F%3E%3CP%3E%7C--Compute%20Scalar(DEFINE%3A(%5BExpr1004%5D%3DCASE%20WHEN%20%5BExpr1005%5D%3D(0)%20THEN%20NULL%20ELSE%20%5BExpr1006%5D%20END))%20%3CBR%20%2F%3E%20%7C--Concatenation%20%3CBR%20%2F%3E%20%7C--Stream%20Aggregate(GROUP%20BY%3A(%5BSales%5D.%5BEmpId%5D%2C%20%5BSales%5D.%5BYr%5D)%20DEFINE%3A(%5BExpr1005%5D%3DSUM(%5BExpr1007%5D)%2C%20%5BExpr1006%5D%3DSUM(%5BExpr1008%5D)))%20%3CBR%20%2F%3E%20%7C%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%7C--Sort(ORDER%20BY%3A(%5BSales%5D.%5BEmpId%5D%20ASC%2C%20%5BSales%5D.%5BYr%5D%20ASC))%20%3CBR%20%2F%3E%20%7C%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%7C--Table%20Spool%20%3CBR%20%2F%3E%20%7C%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%7C--Stream%20Aggregate(GROUP%20BY%3A(%5BSales%5D.%5BYr%5D%2C%20%5BSales%5D.%5BEmpId%5D)%20DEFINE%3A(%5BExpr1007%5D%3DCOUNT_BIG(%5BSales%5D.%5BSales%5D)%2C%20%5BExpr1008%5D%3DSUM(%5BSales%5D.%5BSales%5D)))%20%3CBR%20%2F%3E%20%7C%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%7C--Sort(ORDER%20BY%3A(%5BSales%5D.%5BYr%5D%20ASC%2C%20%5BSales%5D.%5BEmpId%5D%20ASC))%20%3CBR%20%2F%3E%20%7C%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%7C--Table%20Scan(OBJECT%3A(%5BSales%5D))%20%3CBR%20%2F%3E%20%7C--Compute%20Scalar(DEFINE%3A(%5BExpr1012%5D%3DNULL))%20%3CBR%20%2F%3E%20%7C--Stream%20Aggregate(GROUP%20BY%3A(%5BSales%5D.%5BYr%5D)%20DEFINE%3A(%5BExpr1005%5D%3DSUM(%5BExpr1007%5D)%2C%20%5BExpr1006%5D%3DSUM(%5BExpr1008%5D)))%20%3CBR%20%2F%3E%20%7C--Table%20Spool%3C%2FP%3E%3CBR%20%2F%3E%3CP%3EThis%20plan%20consists%20of%20two%20parts.%26nbsp%3B%20SQL%20Server%20has%20effectively%20rewritten%20our%20query%20as%20follows%3A%3C%2FP%3E%3CBR%20%2F%3E%3CBLOCKQUOTE%3E%3CBR%20%2F%3E%3CP%3ESELECT%20EmpId%2C%20Yr%2C%20SUM(Sales)%20AS%20Sales%20%3CBR%20%2F%3E%20FROM%20Sales%20%3CBR%20%2F%3E%20GROUP%20BY%20EmpId%2C%20Yr%20WITH%20ROLLUP%20%3CBR%20%2F%3E%20UNION%20ALL%20%3CBR%20%2F%3E%20SELECT%20NULL%2C%20Yr%2C%20SUM(Sales)%20%3CBR%20%2F%3E%20FROM%20Sales%20%3CBR%20%2F%3E%20GROUP%20BY%20Yr%3C%2FP%3E%0A%20%20%3C%2FBLOCKQUOTE%3E%3CBR%20%2F%3E%3CP%3EThe%20first%20part%20of%20the%20plan%20computes%20the%20result%20for%20the%20WITH%20ROLLUP%20query%20above.%26nbsp%3B%20I%20described%20how%20this%20query%20plan%20works%20in%20last%20week's%20post.%26nbsp%3B%20The%20second%20part%20of%20this%20plan%20computes%20the%20missing%20year%20sub-totals%20yielding%20the%20entire%20CUBE%20result.%26nbsp%3B%20Note%20that%20this%20plan%20employs%20a%20common%20sub-expression%20spool.%26nbsp%3B%20As%20I%20discussed%20in%20%3CA%20href%3D%22http%3A%2F%2Fblogs.msdn.com%2Fcraigfr%2Farchive%2F2007%2F08%2F22%2Foptimized-non-clustered-index-maintenance-in-per-index-plans.aspx%22%20mce_href%3D%22http%3A%2F%2Fblogs.msdn.com%2Fcraigfr%2Farchive%2F2007%2F08%2F22%2Foptimized-non-clustered-index-maintenance-in-per-index-plans.aspx%22%20title%3D%22Optimized%20Non-clustered%20Index%20Maintenance%20in%20Per-Index%20Plans%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3E%20this%20post%20%3C%2FA%3E%20%2C%20a%20common%20sub-expression%20spool%20copies%20its%20input%20rows%20into%20a%20worktable%20and%20then%20reads%20and%20returns%20the%20rows%20from%20the%20worktable%20multiple%20times%20-%20in%20this%20case%20twice.%26nbsp%3B%20The%20spool%20is%20meant%20to%20improve%20performance%20although%2C%20in%20this%20example%2C%20it%20has%20little%20impact%20since%20the%20server%20could%20just%20as%20easily%20have%20re-read%20the%20original%20Sales%20table.%20%26nbsp%3BHowever%2C%20if%20the%20input%20to%20the%20aggregation%20was%20more%20complex%20and%20cost%20more%20to%20evaluate%2C%20the%20spool%20would%20help.%3C%2FP%3E%3CBR%20%2F%3E%3CP%3EIf%20we%20use%20the%20WITH%20CUBE%20clause%20when%20aggregating%20on%20more%20than%20two%20columns%2C%20SQL%20Server%20simply%20generates%20increasingly%20complex%20plans%20with%20additional%20inputs%20to%20the%20concatentation%20operator.%26nbsp%3B%20As%20with%20the%20simple%20two%20column%20example%2C%20the%20idea%20is%20to%20compute%20the%20whole%20CUBE%20by%20computing%20all%20of%20the%20individual%20ROLLUPs%20that%20compose%20it.%3C%2FP%3E%3CBR%20%2F%3E%3CP%3EFinally%2C%20we%20can%20actually%20combine%20WITH%20CUBE%20and%20PIVOT%20to%20generate%20the%20above%20table%20in%20a%20single%20simple%20statement.%26nbsp%3B%20(I%20actually%20proposed%20a%20variation%20of%20this%20query%20in%20an%20answer%20to%20a%20reader's%20%3CA%20href%3D%22http%3A%2F%2Fblogs.msdn.com%2Fcraigfr%2Farchive%2F2007%2F07%2F03%2Fthe-pivot-operator.aspx%23comments%22%20mce_href%3D%22http%3A%2F%2Fblogs.msdn.com%2Fcraigfr%2Farchive%2F2007%2F07%2F03%2Fthe-pivot-operator.aspx%23comments%22%20title%3D%22The%20PIVOT%20Operator%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3E%20comment%20%3C%2FA%3E%20on%20my%20post%20about%20the%20PIVOT%20operator%20but%20I%20like%20this%20solution%20better.)%3C%2FP%3E%3CBR%20%2F%3E%3CBLOCKQUOTE%3E%3CBR%20%2F%3E%3CP%3ESELECT%20EmpId%2C%20%5B2005%5D%2C%20%5B2006%5D%2C%20%5B2007%5D%2C%20%5BALL%5D%20%3CBR%20%2F%3E%20FROM%20%3CBR%20%2F%3E%20(%20%3CBR%20%2F%3E%20SELECT%20%3CBR%20%2F%3E%20CASE%20WHEN%20GROUPING(EmpId)%20%3D%200%20%3CBR%20%2F%3E%20THEN%20CAST%20(EmpId%20AS%20CHAR(7))%20%3CBR%20%2F%3E%20ELSE%20'ALL'%20%3CBR%20%2F%3E%20END%20AS%20EmpId%2C%20%3CBR%20%2F%3E%20CASE%20WHEN%20GROUPING(Yr)%20%3D%200%20%3CBR%20%2F%3E%20THEN%20CAST%20(Yr%20AS%20CHAR(7))%20%3CBR%20%2F%3E%20ELSE%20'ALL'%20%3CBR%20%2F%3E%20END%20AS%20Yr%2C%20%3CBR%20%2F%3E%20SUM(Sales)%20AS%20Sales%20%3CBR%20%2F%3E%20FROM%20Sales%20%3CBR%20%2F%3E%20GROUP%20BY%20EmpId%2C%20Yr%20WITH%20CUBE%20%3CBR%20%2F%3E%20)%20AS%20s%20%3CBR%20%2F%3E%20PIVOT%20(SUM(Sales)%20FOR%20Yr%20IN%20(%5B2005%5D%2C%20%5B2006%5D%2C%20%5B2007%5D%2C%20%5BALL%5D))%20AS%20p%3C%2FP%3E%0A%20%20%3C%2FBLOCKQUOTE%3E%3CBR%20%2F%3E%3CP%3EHere%20is%20the%20output%20from%20this%20query%3A%3C%2FP%3EEmpId%202005%202006%202007%20ALL%20%3CBR%20%2F%3E%20-------%20---------------------%20---------------------%20---------------------%20---------------------%20%3CBR%20%2F%3E%201%2012000.00%2018000.00%2025000.00%2055000.00%20%3CBR%20%2F%3E%202%2015000.00%206000.00%20NULL%2021000.00%20%3CBR%20%2F%3E%203%20NULL%2020000.00%2024000.00%2044000.00%20%3CBR%20%2F%3E%20ALL%2027000.00%2044000.00%2049000.00%20120000.00%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-383320%22%20slang%3D%22en-US%22%3EFirst%20published%20on%20MSDN%20on%20Sep%2027%2C%202007%20In%20my%20last%20post%2C%20I%20wrote%20about%20how%26nbsp%3Baggregation%20WITH%20ROLLUP%20works.%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-383320%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3ESQLServerQueryProcessing%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Version history
Last update:
‎Mar 23 2019 05:08 AM
Updated by: