The examples in this section use the SUM aggregate function so that the result sets can be compared. The other aggregate functions might also be used to calculate different summaries.
A. Using a simple GROUP BY
In the following example, the simple GROUP BY returns a result set to compare to the result sets of examples B through K. These examples use the GROUP BY operators with the same SELECT statement.
USE AdventureWorks;
GO
SELECT T.[Group] AS N'Region', T.CountryRegionCode AS N'Country'
,S.Name AS N'Store', H.SalesPersonID
,SUM(TotalDue) AS N'Total Sales'
FROM Sales.Customer C
INNER JOIN Sales.Store S
ON C.CustomerID = S.CustomerID
INNER JOIN Sales.SalesTerritory T
ON C.TerritoryID = T.TerritoryID
INNER JOIN Sales.SalesOrderHeader H
ON S.CustomerID = H.CustomerID
WHERE T.[Group] = N'Europe'
AND T.CountryRegionCode IN(N'DE', N'FR')
AND H.SalesPersonID IN(284, 286, 289)
AND SUBSTRING(S.Name,1,4)IN(N'Vers', N'Spa ')
GROUP BY T.[Group], T.CountryRegionCode, S.Name, H.SalesPersonID
ORDER BY T.[Group], T.CountryRegionCode
,S.Name,H.SalesPersonID;
Here is the result set.
|
Region
|
Country
|
Store
|
SalesPersonID
|
Total Sales
|
|---|
|
Europe
|
DE
|
Versatile Sporting Goods Company
|
284
|
859.232
|
|
Europe
|
DE
|
Versatile Sporting Goods Company
|
289
|
17691.83
|
|
Europe
|
FR
|
Spa and Exercise Outfitters
|
284
|
32774.36
|
|
Europe
|
FR
|
Spa and Exercise Outfitters
|
286
|
246272.4
|
B. Using GROUP BY ROLLUP
In the following example, the ROLLUP operator returns a result set that contains the following groupings:
-
Region, Country, Store, and SalesPersonID
-
Region, Country, and Store
-
Region, and Country
-
Region
-
grand total
The number of groupings that is generated by ROLLUP is the same as the number of columns in the ROLLUP list plus a grand total grouping. The number of rows in a grouping is determined by the number of unique combinations of values in the columns of the grouping.
USE AdventureWorks;
GO
SELECT T.[Group] AS N'Region', T.CountryRegionCode AS N'Country'
,S.Name AS N'Store', H.SalesPersonID
,SUM(TotalDue) AS N'Total Sales'
FROM Sales.Customer C
INNER JOIN Sales.Store S
ON C.CustomerID = S.CustomerID
INNER JOIN Sales.SalesTerritory T
ON C.TerritoryID = T.TerritoryID
INNER JOIN Sales.SalesOrderHeader H
ON S.CustomerID = H.CustomerID
WHERE T.[Group] = N'Europe'
AND T.CountryRegionCode IN(N'DE', N'FR')
AND H.SalesPersonID IN(284, 286, 289)
AND SUBSTRING(S.Name,1,4)IN(N'Vers', N'Spa ')
GROUP BY ROLLUP(
T.[Group], T.CountryRegionCode, S.Name, H.SalesPersonID)
ORDER BY T.[Group], T.CountryRegionCode, S.Name, H.SalesPersonID;
Here is the result set.
|
Region
|
Country
|
Store
|
SalesPersonID
|
Total Sales
|
|---|
|
NULL
|
NULL
|
NULL
|
NULL
|
297597.8
|
|
Europe
|
NULL
|
NULL
|
NULL
|
297597.8
|
|
Europe
|
DE
|
NULL
|
NULL
|
18551.07
|
|
Europe
|
DE
|
Versatile Sporting Goods Company
|
NULL
|
18551.07
|
|
Europe
|
DE
|
Versatile Sporting Goods Company
|
284
|
859.232
|
|
Europe
|
DE
|
Versatile Sporting Goods Company
|
289
|
17691.83
|
|
Europe
|
FR
|
NULL
|
NULL
|
279046.8
|
|
Europe
|
FR
|
Spa and Exercise Outfitters
|
NULL
|
279046.8
|
|
Europe
|
FR
|
Spa and Exercise Outfitters
|
284
|
32774.36
|
|
Europe
|
FR
|
Spa and Exercise Outfitters
|
286
|
246272.4
|
C. Using GROUP BY ROLLUP with the column order reversed
In the following example, the ROLLUP operator returns a result set that contains the following groupings:
-
SalesPersonID, Store, Country, and Region
-
SalesPersonID, Store, and Country
-
SalesPersonID, and Store
-
SalesPersonID
-
grand total
The columns in the ROLLUP list are the same as those in example B, but they are in the opposite order. Columns are rolled up from right to left; therefore, the order affects the groupings. The number of rows in the result set might vary with the column order.
USE AdventureWorks;
GO
SELECT T.[Group] AS N'Region', T.CountryRegionCode AS N'Country'
,S.Name AS N'Store', H.SalesPersonID
,SUM(TotalDue) AS N'Total Sales'
FROM Sales.Customer C
INNER JOIN Sales.Store S
ON C.CustomerID = S.CustomerID
INNER JOIN Sales.SalesTerritory T
ON C.TerritoryID = T.TerritoryID
INNER JOIN Sales.SalesOrderHeader H
ON S.CustomerID = H.CustomerID
WHERE T.[Group] = N'Europe'
AND T.CountryRegionCode IN(N'DE', N'FR')
AND H.SalesPersonID IN(284, 286, 289)
AND SUBSTRING(S.Name,1,4)IN(N'Vers', N'Spa ')
GROUP BY ROLLUP(
H.SalesPersonID, S.Name, T.CountryRegionCode, T.[Group])
ORDER BY H.SalesPersonID, S.Name, T.CountryRegionCode, T.[Group];
Here is the result set.
|
Region
|
Country
|
Store
|
SalesPersonID
|
Total Sales
|
|---|
|
NULL
|
NULL
|
NULL
|
NULL
|
297597.8
|
|
NULL
|
NULL
|
NULL
|
284
|
33633.59
|
|
NULL
|
NULL
|
Spa and Exercise Outfitters
|
284
|
32774.36
|
|
NULL
|
FR
|
Spa and Exercise Outfitters
|
284
|
32774.36
|
|
Europe
|
FR
|
Spa and Exercise Outfitters
|
284
|
32774.36
|
|
NULL
|
NULL
|
Versatile Sporting Goods Company
|
284
|
859.232
|
|
NULL
|
DE
|
Versatile Sporting Goods Company
|
284
|
859.232
|
|
Europe
|
DE
|
Versatile Sporting Goods Company
|
284
|
859.232
|
|
NULL
|
NULL
|
NULL
|
286
|
246272.4
|
|
NULL
|
NULL
|
Spa and Exercise Outfitters
|
286
|
246272.4
|
|
NULL
|
FR
|
Spa and Exercise Outfitters
|
286
|
246272.4
|
|
Europe
|
FR
|
Spa and Exercise Outfitters
|
286
|
246272.4
|
|
NULL
|
NULL
|
NULL
|
289
|
17691.83
|
|
NULL
|
NULL
|
Versatile Sporting Goods Company
|
289
|
17691.83
|
|
NULL
|
DE
|
Versatile Sporting Goods Company
|
289
|
17691.83
|
|
Europe
|
DE
|
Versatile Sporting Goods Company
|
289
|
17691.83
|
D. Using GROUP BY with concatenated ROLLUP operations
In the following example, the cross product of the two ROLLUP operations is returned.
USE AdventureWorks;
GO
SELECT T.[Group] AS N'Region', T.CountryRegionCode AS N'Country'
,DATEPART(yyyy,OrderDate) AS 'Year'
,DATEPART(mm,OrderDate) AS 'Month'
,SUM(TotalDue) AS N'Total Sales'
FROM Sales.Customer C
INNER JOIN Sales.Store S
ON C.CustomerID = S.CustomerID
INNER JOIN Sales.SalesTerritory T
ON C.TerritoryID = T.TerritoryID
INNER JOIN Sales.SalesOrderHeader H
ON S.CustomerID = H.CustomerID
WHERE T.[Group] = N'Europe'
AND T.CountryRegionCode IN(N'DE', N'FR')
AND DATEPART(yyyy,OrderDate) = '2004'
GROUP BY
ROLLUP(T.[Group], T.CountryRegionCode)
,ROLLUP(DATEPART(yyyy,OrderDate), DATEPART(mm,OrderDate))
ORDER BY T.[Group], T.CountryRegionCode
,DATEPART(yyyy,OrderDate), DATEPART(mm,OrderDate);
Here is the result set.
|
Region
|
Country
|
Year
|
Month
|
Total Sales
|
|---|
|
NULL
|
NULL
|
NULL
|
NULL
|
3031201
|
|
NULL
|
NULL
|
2004
|
NULL
|
3031201
|
|
NULL
|
NULL
|
2004
|
1
|
208553.6
|
|
NULL
|
NULL
|
2004
|
2
|
819466.6
|
|
NULL
|
NULL
|
2004
|
3
|
298579.1
|
|
NULL
|
NULL
|
2004
|
4
|
294427.7
|
|
NULL
|
NULL
|
2004
|
5
|
1070679
|
|
NULL
|
NULL
|
2004
|
6
|
339495.1
|
|
Europe
|
NULL
|
NULL
|
NULL
|
3031201
|
|
Europe
|
NULL
|
2004
|
NULL
|
3031201
|
|
Europe
|
NULL
|
2004
|
1
|
208553.6
|
|
Europe
|
NULL
|
2004
|
2
|
819466.6
|
|
Europe
|
NULL
|
2004
|
3
|
298579.1
|
|
Europe
|
NULL
|
2004
|
4
|
294427.7
|
|
Europe
|
NULL
|
2004
|
5
|
1070679
|
|
Europe
|
NULL
|
2004
|
6
|
339495.1
|
|
Europe
|
DE
|
NULL
|
NULL
|
1196260
|
|
Europe
|
DE
|
2004
|
NULL
|
1196260
|
|
Europe
|
DE
|
2004
|
1
|
155066.2
|
|
Europe
|
DE
|
2004
|
2
|
197801.8
|
|
Europe
|
DE
|
2004
|
3
|
180977.7
|
|
Europe
|
DE
|
2004
|
4
|
222683.4
|
|
Europe
|
DE
|
2004
|
5
|
258962
|
|
Europe
|
DE
|
2004
|
6
|
180769.1
|
|
Europe
|
FR
|
NULL
|
NULL
|
1834941
|
|
Europe
|
FR
|
2004
|
NULL
|
1834941
|
|
Europe
|
FR
|
2004
|
1
|
53487.37
|
|
Europe
|
FR
|
2004
|
2
|
621664.9
|
|
Europe
|
FR
|
2004
|
3
|
117601.4
|
|
Europe
|
FR
|
2004
|
4
|
71744.28
|
|
Europe
|
FR
|
2004
|
5
|
811716.9
|
|
Europe
|
FR
|
2004
|
6
|
158726
|
E. Using GROUP BY CUBE
In the following example, the CUBE operator returns a result set that has one grouping for all possible combinations of columns in the CUBE list and a grand total grouping.
USE AdventureWorks;
GO
SELECT T.[Group] AS N'Region', T.CountryRegionCode AS N'Country'
,S.Name AS N'Store', H.SalesPersonID
,SUM(TotalDue) AS N'Total Sales'
FROM Sales.Customer C
INNER JOIN Sales.Store S
ON C.CustomerID = S.CustomerID
INNER JOIN Sales.SalesTerritory T
ON C.TerritoryID = T.TerritoryID
INNER JOIN Sales.SalesOrderHeader H
ON S.CustomerID = H.CustomerID
WHERE T.[Group] = N'Europe'
AND T.CountryRegionCode IN(N'DE', N'FR')
AND H.SalesPersonID IN(284, 286, 289)
AND SUBSTRING(S.Name,1,4)IN(N'Vers', N'Spa ')
GROUP BY CUBE(
T.[Group], T.CountryRegionCode, S.Name, H.SalesPersonID)
ORDER BY T.[Group], T.CountryRegionCode, S.Name, H.SalesPersonID;
Here is the result set.
|
Region
|
Country
|
Store
|
SalesPersonID
|
Total Sales
|
|---|
|
NULL
|
NULL
|
NULL
|
NULL
|
297597.8
|
|
NULL
|
NULL
|
NULL
|
284
|
33633.59
|
|
NULL
|
NULL
|
NULL
|
286
|
246272.4
|
|
NULL
|
NULL
|
NULL
|
289
|
17691.83
|
|
NULL
|
NULL
|
Spa and Exercise Outfitters
|
NULL
|
279046.8
|
|
NULL
|
NULL
|
Spa and Exercise Outfitters
|
284
|
32774.36
|
|
NULL
|
NULL
|
Spa and Exercise Outfitters
|
286
|
246272.4
|
|
NULL
|
NULL
|
Versatile Sporting Goods Company
|
NULL
|
18551.07
|
|
NULL
|
NULL
|
Versatile Sporting Goods Company
|
284
|
859.232
|
|
NULL
|
NULL
|
Versatile Sporting Goods Company
|
289
|
17691.83
|
|
NULL
|
DE
|
NULL
|
NULL
|
18551.07
|
|
NULL
|
DE
|
NULL
|
284
|
859.232
|
|
NULL
|
DE
|
NULL
|
289
|
17691.83
|
|
NULL
|
DE
|
Versatile Sporting Goods Company
|
NULL
|
18551.07
|
|
NULL
|
DE
|
Versatile Sporting Goods Company
|
284
|
859.232
|
|
NULL
|
DE
|
Versatile Sporting Goods Company
|
289
|
17691.83
|
|
NULL
|
FR
|
NULL
|
NULL
|
279046.8
|
|
NULL
|
FR
|
NULL
|
284
|
32774.36
|
|
NULL
|
FR
|
NULL
|
286
|
246272.4
|
|
NULL
|
FR
|
Spa and Exercise Outfitters
|
NULL
|
279046.8
|
|
NULL
|
FR
|
Spa and Exercise Outfitters
|
284
|
32774.36
|
|
NULL
|
FR
|
Spa and Exercise Outfitters
|
286
|
246272.4
|
|
Europe
|
NULL
|
NULL
|
NULL
|
297597.8
|
|
Europe
|
NULL
|
NULL
|
284
|
33633.59
|
|
Europe
|
NULL
|
NULL
|
286
|
246272.4
|
|
Europe
|
NULL
|
NULL
|
289
|
17691.83
|
|
Europe
|
NULL
|
Spa and Exercise Outfitters
|
NULL
|
279046.8
|
|
Europe
|
NULL
|
Spa and Exercise Outfitters
|
284
|
32774.36
|
|
Europe
|
NULL
|
Spa and Exercise Outfitters
|
286
|
246272.4
|
|
Europe
|
NULL
|
Versatile Sporting Goods Company
|
NULL
|
18551.07
|
|
Europe
|
NULL
|
Versatile Sporting Goods Company
|
284
|
859.232
|
|
Europe
|
NULL
|
Versatile Sporting Goods Company
|
289
|
17691.83
|
|
Europe
|
DE
|
NULL
|
NULL
|
18551.07
|
|
Europe
|
DE
|
NULL
|
284
|
859.232
|
|
Europe
|
DE
|
NULL
|
289
|
17691.83
|
|
Europe
|
DE
|
Versatile Sporting Goods Company
|
NULL
|
18551.07
|
|
Europe
|
DE
|
Versatile Sporting Goods Company
|
284
|
859.232
|
|
Europe
|
DE
|
Versatile Sporting Goods Company
|
289
|
17691.83
|
|
Europe
|
FR
|
NULL
|
NULL
|
279046.8
|
|
Europe
|
FR
|
NULL
|
284
|
32774.36
|
|
Europe
|
FR
|
NULL
|
286
|
246272.4
|
|
Europe
|
FR
|
Spa and Exercise Outfitters
|
NULL
|
279046.8
|
|
Europe
|
FR
|
Spa and Exercise Outfitters
|
284
|
32774.36
|
|
Europe
|
FR
|
Spa and Exercise Outfitters
|
286
|
246272.4
|
F. Using CUBE with composite elements
In the following example, the CUBE operator returns a result set that has one grouping for all possible combinations of columns in the CUBE list and a grand total grouping.
The operator processes the grouped columns (T.[Group], T.CountryRegionCode) and (DATEPART(yyyy,OrderDate), DATEPART(mm,OrderDate)) each as a single column.
USE AdventureWorks;
GO
SELECT T.[Group] AS N'Region', T.CountryRegionCode AS N'Country'
,DATEPART(yyyy,OrderDate) AS 'Year'
,DATEPART(mm,OrderDate) AS 'Month'
,SUM(TotalDue) AS N'Total Sales'
FROM Sales.Customer C
INNER JOIN Sales.Store S
ON C.CustomerID = S.CustomerID
INNER JOIN Sales.SalesTerritory T
ON C.TerritoryID = T.TerritoryID
INNER JOIN Sales.SalesOrderHeader H
ON S.CustomerID = H.CustomerID
WHERE T.[Group] = N'Europe'
AND T.CountryRegionCode IN(N'DE', N'FR')
AND DATEPART(yyyy,OrderDate) = '2004'
GROUP BY CUBE(
(T.[Group], T.CountryRegionCode)
,(DATEPART(yyyy,OrderDate), DATEPART(mm,OrderDate)))
ORDER BY T.[Group], T.CountryRegionCode
,DATEPART(yyyy,OrderDate), DATEPART(mm,OrderDate);
Here is the result set.
|
Region
|
Country
|
Year
|
Month
|
Total Sales
|
|---|
|
NULL
|
NULL
|
NULL
|
NULL
|
3031201
|
|
NULL
|
NULL
|
2004
|
1
|
208553.6
|
|
NULL
|
NULL
|
2004
|
2
|
819466.6
|
|
NULL
|
NULL
|
2004
|
3
|
298579.1
|
|
NULL
|
NULL
|
2004
|
4
|
294427.7
|
|
NULL
|
NULL
|
2004
|
5
|
1070679
|
|
NULL
|
NULL
|
2004
|
6
|
339495.1
|
|
Europe
|
DE
|
NULL
|
NULL
|
1196260
|
|
Europe
|
DE
|
2004
|
1
|
155066.2
|
|
Europe
|
DE
|
2004
|
2
|
197801.8
|
|
Europe
|
DE
|
2004
|
3
|
180977.7
|
|
Europe
|
DE
|
2004
|
4
|
222683.4
|
|
Europe
|
DE
|
2004
|
5
|
258962
|
|
Europe
|
DE
|
2004
|
6
|
180769.1
|
|
Europe
|
FR
|
NULL
|
NULL
|
1834941
|
|
Europe
|
FR
|
2004
|
1
|
53487.37
|
|
Europe
|
FR
|
2004
|
2
|
621664.9
|
|
Europe
|
FR
|
2004
|
3
|
117601.4
|
|
Europe
|
FR
|
2004
|
4
|
71744.28
|
|
Europe
|
FR
|
2004
|
5
|
811716.9
|
|
Europe
|
FR
|
2004
|
6
|
158726
|
G. Using GROUP BY with GROUPING SETS
In the following example, the GROUPING SETS operator has four groupings, one for each column in the SELECT list. The operator returns one row for each unique value in the Region, Country, Store, and SalesPersonID columns.
USE AdventureWorks;
GO
SELECT T.[Group] AS N'Region', T.CountryRegionCode AS N'Country'
,S.Name AS N'Store', H.SalesPersonID
,SUM(TotalDue) AS N'Total Sales'
FROM Sales.Customer C
INNER JOIN Sales.Store S
ON C.CustomerID = S.CustomerID
INNER JOIN Sales.SalesTerritory T
ON C.TerritoryID = T.TerritoryID
INNER JOIN Sales.SalesOrderHeader H
ON S.CustomerID = H.CustomerID
WHERE T.[Group] = N'Europe'
AND T.CountryRegionCode IN(N'DE', N'FR')
AND H.SalesPersonID IN(284, 286, 289)
AND SUBSTRING(S.Name,1,4)IN(N'Vers', N'Spa ')
GROUP BY GROUPING SETS
(T.[Group], T.CountryRegionCode, S.Name, H.SalesPersonID)
ORDER BY T.[Group], T.CountryRegionCode, S.Name, H.SalesPersonID;
Here is the result set.
|
Region
|
Country
|
Store
|
SalesPersonID
|
Total Sales
|
|---|
|
NULL
|
NULL
|
NULL
|
284
|
33633.59
|
|
NULL
|
NULL
|
NULL
|
286
|
246272.4
|
|
NULL
|
NULL
|
NULL
|
289
|
17691.83
|
|
NULL
|
NULL
|
Spa and Exercise Outfitters
|
NULL
|
279046.8
|
|
NULL
|
NULL
|
Versatile Sporting Goods Company
|
NULL
|
18551.07
|
|
NULL
|
DE
|
NULL
|
NULL
|
18551.07
|
|
NULL
|
FR
|
NULL
|
NULL
|
279046.8
|
|
Europe
|
NULL
|
NULL
|
NULL
|
297597.8
|
H. Using GROUPING SETS with composite elements
In the following example, the GROUPING SETS list contains two composite elements, (T.[Group], T.CountryRegionCode) and (DATEPART(yyyy,OrderDate), DATEPART(mm,OrderDate)). Each composite element is treated as one column.
USE AdventureWorks;
GO
SELECT T.[Group] AS N'Region', T.CountryRegionCode AS N'Country'
,DATEPART(yyyy,OrderDate) AS 'Year'
,DATEPART(mm,OrderDate) AS 'Month'
,SUM(TotalDue) AS N'Total Sales'
FROM Sales.Customer C
INNER JOIN Sales.Store S
ON C.CustomerID = S.CustomerID
INNER JOIN Sales.SalesTerritory T
ON C.TerritoryID = T.TerritoryID
INNER JOIN Sales.SalesOrderHeader H
ON S.CustomerID = H.CustomerID
WHERE T.[Group] = N'Europe'
AND T.CountryRegionCode IN(N'DE', N'FR')
AND DATEPART(yyyy,OrderDate) = '2004'
GROUP BY GROUPING SETS(
(T.[Group], T.CountryRegionCode)
,(DATEPART(yyyy,OrderDate), DATEPART(mm,OrderDate)))
ORDER BY T.[Group], T.CountryRegionCode
,DATEPART(yyyy,OrderDate), DATEPART(mm,OrderDate);
Here is the result set.
|
Region
|
Country
|
Year
|
Month
|
Total Sales
|
|---|
|
NULL
|
NULL
|
2004
|
1
|
208553.6
|
|
NULL
|
NULL
|
2004
|
2
|
819466.6
|
|
NULL
|
NULL
|
2004
|
3
|
298579.1
|
|
NULL
|
NULL
|
2004
|
4
|
294427.7
|
|
NULL
|
NULL
|
2004
|
5
|
1070679
|
|
NULL
|
NULL
|
2004
|
6
|
339495.1
|
|
Europe
|
DE
|
NULL
|
NULL
|
1196260
|
|
Europe
|
FR
|
NULL
|
NULL
|
1834941
|
I. Using GROUP BY with multiple GROUPING SETS
In the following example, the GROUPING SETS list has five elements. The result set has one row for the following elements:
-
Each unique combination of values in the
Region and Country columns
-
Each unique value in the
Store column
-
Each unique combination of values in the
SalesPersonID and Region columns
-
Each unique value in the
SalesPersonID column
-
A grand total
USE AdventureWorks;
GO
SELECT T.[Group] AS N'Region', T.CountryRegionCode AS N'Country'
,S.Name AS N'Store', H.SalesPersonID
,SUM(TotalDue) AS N'Total Sales'
FROM Sales.Customer C
INNER JOIN Sales.Store S
ON C.CustomerID = S.CustomerID
INNER JOIN Sales.SalesTerritory T
ON C.TerritoryID = T.TerritoryID
INNER JOIN Sales.SalesOrderHeader H
ON S.CustomerID = H.CustomerID
WHERE T.[Group] = N'Europe'
AND T.CountryRegionCode IN(N'DE', N'FR')
AND H.SalesPersonID IN(284, 286, 289)
AND SUBSTRING(S.Name,1,4)IN(N'Vers', N'Spa ')
GROUP BY GROUPING SETS(
(T.[Group], T.CountryRegionCode)
,(S.Name)
,(H.SalesPersonID,T.[Group])
,(H.SalesPersonID)
,())
ORDER BY T.[Group], T.CountryRegionCode, S.Name, H.SalesPersonID;
Here is the result set.
|
Region
|
Country
|
Store
|
SalesPersonID
|
Total Sales
|
|---|
|
NULL
|
NULL
|
NULL
|
NULL
|
297597.8
|
|
NULL
|
NULL
|
NULL
|
284
|
33633.59
|
|
NULL
|
NULL
|
NULL
|
286
|
246272.4
|
|
NULL
|
NULL
|
NULL
|
289
|
17691.83
|
|
NULL
|
NULL
|
Spa and Exercise Outfitters
|
NULL
|
279046.8
|
|
NULL
|
NULL
|
Versatile Sporting Goods Company
|
NULL
|
18551.07
|
|
Europe
|
NULL
|
NULL
|
284
|
33633.59
|
|
Europe
|
NULL
|
NULL
|
286
|
246272.4
|
|
Europe
|
NULL
|
NULL
|
289
|
17691.83
|
|
Europe
|
DE
|
NULL
|
NULL
|
18551.07
|
|
Europe
|
FR
|
NULL
|
NULL
|
279046.8
|
J. Using GROUPING SETS with a ROLLUP of part of the GROUP BY list
In the following example, the GROUPING SETS list includes groupings for columns T.[Group] and T.CountryRegionCode and a ROLLUP of columns S.Name and H.SalesPersonID.
USE AdventureWorks;
GO
SELECT T.[Group] AS N'Region', T.CountryRegionCode AS N'Country'
,S.Name AS N'Store', H.SalesPersonID
,SUM(TotalDue) AS N'Total Sales'
FROM Sales.Customer C
INNER JOIN Sales.Store S
ON C.CustomerID = S.CustomerID
INNER JOIN Sales.SalesTerritory T
ON C.TerritoryID = T.TerritoryID
INNER JOIN Sales.SalesOrderHeader H
ON S.CustomerID = H.CustomerID
WHERE T.[Group] = N'Europe'
AND T.CountryRegionCode IN(N'DE', N'FR')
AND H.SalesPersonID IN(284, 286, 289)
AND SUBSTRING(S.Name,1,4)IN(N'Vers', N'Spa ')
GROUP BY GROUPING SETS(
T.[Group], T.CountryRegionCode
,ROLLUP(S.Name, H.SalesPersonID))
ORDER BY T.[Group], T.CountryRegionCode, S.Name, H.SalesPersonID;
Here is the result set.
|
Region
|
Country
|
Store
|
SalesPersonID
|
Total Sales
|
|---|
|
NULL
|
NULL
|
NULL
|
NULL
|
297597.8
|
|
NULL
|
NULL
|
Spa and Exercise Outfitters
|
NULL
|
279046.8
|
|
NULL
|
NULL
|
Spa and Exercise Outfitters
|
284
|
32774.36
|
|
NULL
|
NULL
|
Spa and Exercise Outfitters
|
286
|
246272.4
|
|
NULL
|
NULL
|
Versatile Sporting Goods Company
|
NULL
|
18551.07
|
|
NULL
|
NULL
|
Versatile Sporting Goods Company
|
284
|
859.232
|
|
NULL
|
NULL
|
Versatile Sporting Goods Company
|
289
|
17691.83
|
|
NULL
|
DE
|
NULL
|
NULL
|
18551.07
|
|
NULL
|
FR
|
NULL
|
NULL
|
279046.8
|
|
Europe
|
NULL
|
NULL
|
NULL
|
297597.8
|
K. Using GROUPING SETS with a CUBE of part of the GROUP BY list
In the following example, the GROUPING SETS list includes groupings for columns T.[Group] and T.CountryRegionCode and a CUBE of columns S.Name and H.SalesPersonID.
USE AdventureWorks;
GO
SELECT T.[Group] AS N'Region', T.CountryRegionCode AS N'Country'
,S.Name AS N'Store', H.SalesPersonID
,SUM(TotalDue) AS N'Total Sales'
FROM Sales.Customer C
INNER JOIN Sales.Store S
ON C.CustomerID = S.CustomerID
INNER JOIN Sales.SalesTerritory T
ON C.TerritoryID = T.TerritoryID
INNER JOIN Sales.SalesOrderHeader H
ON S.CustomerID = H.CustomerID
WHERE T.[Group] = N'Europe'
AND T.CountryRegionCode IN(N'DE', N'FR')
AND H.SalesPersonID IN(284, 286, 289)
AND SUBSTRING(S.Name,1,4)IN(N'Vers', N'Spa ')
GROUP BY GROUPING SETS(
T.[Group], T.CountryRegionCode
,CUBE(S.Name, H.SalesPersonID))
ORDER BY T.[Group], T.CountryRegionCode, S.Name, H.SalesPersonID;
Here is the result set.
|
Region
|
Country
|
Store
|
SalesPersonID
|
Total Sales
|
|---|
|
NULL
|
NULL
|
NULL
|
NULL
|
297597.8
|
|
NULL
|
NULL
|
NULL
|
284
|
33633.59
|
|
NULL
|
NULL
|
NULL
|
286
|
246272.4
|
|
NULL
|
NULL
|
NULL
|
289
|
17691.83
|
|
NULL
|
NULL
|
Spa and Exercise Outfitters
|
NULL
|
279046.8
|
|
NULL
|
NULL
|
Spa and Exercise Outfitters
|
284
|
32774.36
|
|
NULL
|
NULL
|
Spa and Exercise Outfitters
|
286
|
246272.4
|
|
NULL
|
NULL
|
Versatile Sporting Goods Company
|
NULL
|
18551.07
|
|
NULL
|
NULL
|
Versatile Sporting Goods Company
|
284
|
859.232
|
|
NULL
|
NULL
|
Versatile Sporting Goods Company
|
289
|
17691.83
|
|
NULL
|
DE
|
NULL
|
NULL
|
18551.07
|
|
NULL
|
FR
|
NULL
|
NULL
|
279046.8
|
|
Europe
|
NULL
|
NULL
|
NULL
|
297597.8
|