SQL Server administration and T-SQL development, Web Programming with ASP.NET, HTML5 and Javascript, Windows Phone 8 app development, SAP Smartforms and ABAP Programming, Windows 7, Visual Studio and MS Office software SQL Server and T-SQL Development Tutorials
Development resources, articles, tutorials, samples, codes and tools for .Net, SQL Server, Windows, Windows Phone, SAP and ABAP, like SAP UI5, Screen Personas, etc.




download SQL Server 2017
download SQL Server 2016
download SQL Server 2014



T-SQL WITH CUBE Example in SQL Server

SQL/OLAP provides methods and tools to analyze data using SQL.
SQL/OLAP enhancements in T-SQL for Business Intelligence solutions with SQL Server 2005 can be listed as follows :
- Ranking functions,
- TOP n,
- Pivot and UnPivot, and
- CUBE and ROLLUP operator.

Let's start this tutorial with sql WITH CUBE T-SQL OLAP command in SQL Server 2005 and later T-SQL.


T-SQL CUBE Operator

The SQL OLAP operator we will give examples here in this sql tutorial is Cube operator in SQL Server 2005
T-SQL Cube operator is very similar to SQL Group By operator with only difference of additional aggregate summary lines.

T-SQL developers know that Group By sql operator takes a list of sql columns that define the groups on which the aggregation functions like SUM() will be calculated.
The columns listed in Group By clause take place in the result set of the SQL Select statement beside the aggregate columns.

SQL Cube operator adds some additional rows to this resultset. And the select resultset turns into an OLAP cube where sql developers can also find the aggregation results on Group By columns. That is like aggregation on aggregated results.





T-SQL CUBE Example

In a software development company, a project manager has reported the following resource usage giving details on which developer has how much worked on which tasks.

To visualize this T-SQL Cube example, let's first create the below sql table to insert sample data.
Then execute the sql Insert statement to populate sql table with sample data for t-sql WITH Cube example.
Careful eyes will catch that the sql insert command is the new syntax that is new with T-SQL enhancements in SQL Server 2008.

Create Table ProjectWorks
(
 [Year] int,
 [Month] tinyint,
 [Project Resource] varchar(20),
 [Project Task] varchar(20),
 [Work Man-Hour] int
)

Insert Into ProjectWorks Values
(2010, 10, 'Developer 1', 'T-SQL', 80),
(2010, 10, 'Developer 1', 'ASP.NET', 30),
(2010, 10, 'Developer 1', 'Unit Test', 90),
(2010, 10, 'Developer 2', 'T-SQL', 30),
(2010, 10, 'Developer 2', 'ASP.NET', 60),
(2010, 10, 'Developer 2', 'Unit Test', 90),
(2010, 10, 'Developer 3', 'T-SQL', 50),
(2010, 10, 'Developer 3', 'ASP.NET', 70),
(2010, 10, 'Developer 3', 'Unit Test', 60),
(2010, 11, 'Developer 1', 'T-SQL', 90),
(2010, 11, 'Developer 1', 'ASP.NET', 40),
(2010, 11, 'Developer 1', 'Unit Test', 60),
(2010, 11, 'Developer 2', 'T-SQL', 20),
(2010, 11, 'Developer 2', 'ASP.NET', 80),
(2010, 11, 'Developer 2', 'Unit Test', 80),
(2010, 11, 'Developer 3', 'T-SQL', 60),
(2010, 11, 'Developer 3', 'ASP.NET', 60),
(2010, 11, 'Developer 3', 'Unit Test', 70)

Below, t-sql developers can find a basic sql WITH CUBE query which also helps developers to figure out the tsql WITH CUBE syntax.
Please note that the WITH CUBE operator is used right after GROUP BY columns list in sql queries.

SELECT
 [Project Resource], [Project Task], SUM([Work Man-Hour]) AS Work
FROM ProjectWorks
GROUP BY
 [Project Resource], [Project Task]
WITH CUBE

The output of the above T-SQL SELECT query using WITH CUBE is as follows :

t-sql-with-cube-example-in-sql-server

Using the above sample sql data, t-sql developers and Business Intelligence developers can create more detailed OLAP report data using the below t-sql CUBE command.
This sql SELECT query using WITH CUBE operator will return aggregate data for time dimension as well.

SELECT
 [Year], [Month],
 [Project Resource], [Project Task],
 SUM([Work Man-Hour]) AS Work
FROM ProjectWorks
GROUP BY
 [Year], [Month], [Project Resource], [Project Task]
WITH CUBE

As a summary, Microsoft SQL Server has introduced With Cube, With Rollup, and similar SQL OLAP commands for use of Business Intelligence and SQL Cube structures in T-SQL and in SQL queries by sql developers.






Related SQL Resources

SQL Server Articles

SQL Server 2012

SQL Server Tools

SQL Blog

MS SQL Server Forums









Copyright © 2004 - 2017 Eralper YILMAZ. All rights reserved.
Community Server by Telligent Systems