krebshack krebshack - 2 months ago 14
SQL Question

Understanding the differences between CUBE and ROLLUP

My assignment asked me to find out "how many invoices are written for each date?"

I was a little stuck and asked my professor for help. She emailed me a query that would answer the question, "How many stoves of each type and version have been built?
For a challenge but no extra points, include the total number of stoves."

This was the query she sent me:

SELECT STOVE.Type + STOVE.Version AS 'Type+Version'
, COUNT(*) AS 'The Count'
FROM STOVE
GROUP BY STOVE.Type + STOVE.Version WITH ROLLUP;


So, I tweaked that query until it met my needs. This is what I came up with:

SELECT InvoiceDt
, COUNT(InvoiceNbr) AS 'Number of Invoices'
FROM INVOICE
GROUP BY InvoiceDt WITH ROLLUP
ORDER BY InvoiceDt ASC;


And it returned the following results that I wanted.

Anyway, I decided to read up on the ROLLUP clause and started with an article from Microsoft. It said that the ROLLUP clause was similar to the CUBE clause but that it was distinguished from the CUBE clause in the following way:


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

  2. ROLLUP generates a result set that shows aggregates for a hierarchy of values in the selected columns.



So, I decided to replace the ROLLUP in my query with CUBE to see what would happen. They produced the same results. I guess that's where I'm getting confused.

It seems like, if you're using the type of query that I am here, that there isn't any practical difference between the two clauses. Is that right? Or, am I not understanding something? I had thought, when I finished reading the Microsoft article, that my results should've been different using the CUBE clause.

Answer

You won't see any difference since you're only rolling up a single column. Consider an example where we do

ROLLUP (YEAR, MONTH, DAY)

With a ROLLUP, it will have the following outputs:

YEAR, MONTH, DAY
YEAR, MONTH
YEAR
()

With CUBE, it will have the following:

YEAR, MONTH, DAY
YEAR, MONTH
YEAR, DAY
YEAR
MONTH, DAY
MONTH
DAY
()

CUBE essentially contains every possible rollup scenario for each node whereas ROLLUP will keep the hierarchy in tact (so it won't skip MONTH and show YEAR/DAY, whereas CUBE will)

This is why you didn't see a difference since you only had a single column you were rolling up.

Hope that helps.