Solved

What are ROLLUP and CUBE commands in SQL Server 2000 ?

Posted on 2006-11-20
1
3,001 Views
Last Modified: 2008-09-22
Can you please explain me what are

ROLLUP & CUBE

commands in SQL Server 2000 ? I need in simple steps in plain english so that i can understand better !!

And what are the differences between

COMPUTE & COMPUTE BY when compared to ROLLUP and CUBE !!
0
Comment
Question by:kishore_peddi
1 Comment
 
LVL 29

Accepted Solution

by:
Gautham Janardhan earned 500 total points
Comment Utility
From BOL,

The ROLLUP operator is useful in generating reports that contain subtotals and totals

The differences between CUBE and ROLLUP are:

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


ROLLUP generates a result set showing aggregates for a hierarchy of values in the selected columns.

For example, a simple table Inventory contains:

Item                 Color                Quantity                  
-------------------- -------------------- --------------------------
Table                Blue                 124                        
Table                Red                  223                        
Chair                Blue                 101                        
Chair                Red                  210                        

This query generates a subtotal report:

SELECT CASE WHEN (GROUPING(Item) = 1) THEN 'ALL'
            ELSE ISNULL(Item, 'UNKNOWN')
       END AS Item,
       CASE WHEN (GROUPING(Color) = 1) THEN 'ALL'
            ELSE ISNULL(Color, 'UNKNOWN')
       END AS Color,
       SUM(Quantity) AS QtySum
FROM Inventory
GROUP BY Item, Color WITH ROLLUP

Item                 Color                QtySum                    
-------------------- -------------------- --------------------------
Chair                Blue                 101.00                    
Chair                Red                  210.00                    
Chair                ALL                  311.00                    
Table                Blue                 124.00                    
Table                Red                  223.00                    
Table                ALL                  347.00                    
ALL                  ALL                  658.00


If the ROLLUP keyword in the query is changed to CUBE, the CUBE result set is the same, except these two additional rows are returned at the end:

ALL                  Blue                 225.00                    
ALL                  Red                  433.00  


The result set of a ROLLUP operation has functionality similar to that returned by a COMPUTE BY; however, ROLLUP has these advantages:

ROLLUP returns a single result set; COMPUTE BY returns multiple result sets that increase the complexity of application code.


ROLLUP can be used in a server cursor; COMPUTE BY cannot.


The query optimizer can sometimes generate more efficient execution plans for ROLLUP than it can for COMPUTE BY.

0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Join & Write a Comment

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now