Solved

What are ROLLUP and CUBE commands in SQL Server 2000 ?

Posted on 2006-11-20
1
3,038 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 29

Accepted Solution

by:
Gautham Janardhan earned 500 total points
ID: 17984918
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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

623 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