Cube in SQL Server
group by cube in sql server 2008
cube group by sql
sql server group by cube example
sql server cube group by
Cube() in SQL Server produces the result set by generating all combinations of columns specified in GROUP BY CUBE().
Let us understand Cube() in SQL Server with examples. Write a query to retrieve Sum of Salary grouped by all combinations of the following 2 columns as well as Grand Total.
Country,
Gender
Using Cube with Group By
SELECT Country, Gender, SUM(Salary) AS TotalSalary
FROM Employees
GROUP BY Cube(Country, Gender)
–OR
SELECT Country, Gender, SUM(Salary) AS TotalSalary
FROM Employees
GROUP BY Country, Gender with Cube
The above query is equivalent to the following Grouping Sets query.
SELECT Country, Gender, SUM(Salary) AS TotalSalary
FROM Employees
GROUP BY
GROUPING SETS
(
(Country, Gender),
(Country),
(Gender),
()
)
The above query is equivalent to the following UNION ALL query. While the data in the result set is the same, the ordering is not. Use ORDER BY to control the ordering of rows in the result set.
SELECT Country, Gender, SUM(Salary) AS TotalSalary
FROM Employees
GROUP BY Country, Gender
UNION ALL
SELECT Country, NULL, SUM(Salary) AS TotalSalary
FROM Employees
GROUP BY Country
UNION ALL
SELECT NULL, Gender, SUM(Salary) AS TotalSalary
FROM Employees
GROUP BY Gender
UNION ALL
SELECT NULL, NULL, SUM(Salary) AS TotalSalary
FROM Employees
Text version of the video
http://csharp-video-tutorials.blogspot.com/2015/09/cube-in-sql-server.html
Slides
http://csharp-video-tutorials.blogspot.com/2015/09/cube-in-sql-server_22.html
All SQL Server Text Articles
http://csharp-video-tutorials.blogspot.com/p/free-sql-server-video-tutorials-for.html
All SQL Server Slides
http://csharp-video-tutorials.blogspot.com/p/sql-server.html
All Dot Net and SQL Server Tutorials in English
https://www.youtube.com/user/kudvenkat/playlists?view=1&sort=dd
All Dot Net and SQL Server Tutorials in Arabic
https://www.youtube.com/c/KudvenkatArabic/playlists
Social