SIGN UP MEMBER LOGIN:
   
ARTICLE

How to use cube command in SQL Server

Posted by Rohatash Articles | SQL Server October 13, 2010
In this article we will learn how to use cube command in SQL Server.
Reader Level:

In this article we will learn how to use cube command in SQL Server.

Cube command

CUBE generates a result set showing aggregates for all combination's of values in the selected columns.

Creating table named sale

CREATE TABLE Sale (EmpId INT, Yr INT, Sales int)

go

INSERT Sale VALUES(1, 2005, 14000)

go

INSERT Sale VALUES(1, 2009, 18000)

go

INSERT Sale VALUES(1, 2007, 25000)

go

INSERT Sale VALUES(2, 2005, 15000)

go

INSERT Sale VALUES(2, 2009, 6000)

go

INSERT Sale VALUES(3, 2009, 20000)

go

INSERT Sale VALUES(3, 2007, 24000)

go

select * from Sale

OUTPUT

tab1.gif

Using cube command

SELECT empid,yr, SUM(sales) as salesum

FROM sale

GROUP BY empid,yr

WITH CUBE

OUTPUT

 tab2.gif

Login to add your contents and source code to this article
This Feature is Sponsored By
post comment
COMMENT USING