Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding ROLLUP in SQL

Tags:

sql

I've figured out CUBE as just generating all the permutations, but I am having trouble with ROLLUP. There don't seem to be any good resources online or in the book I'm reading for explaining SQL for people like me who struggle with it.

My book says that ROLLUP is a special case of the CUBE operator that excludes all cases that don't follow a hierarchy within the results.

I'm not entirely sure what it means, but running it on a table I made kinda produces some useful results.

I made a table from another page on google like this:

Type        Store       Number
Dog     Miami       12
Cat     Miami       18
Turtle   Tampa       4
Dog     Tampa       14
Cat     Naples      9
Dog     Naples      5
Turtle   Naples      1

Then here is query I made:

select store,[type], SUM(number) as Number from pets

group by store, [type]

with rollup

This shows me the number of each type of pet in each store, and total pets in each store, which is kinda cool. If I want to see the query based on pets, I found I have to switch the group by order around so type comes first.

So is rollup based on the first group by clause?

The other question is, I read you use ROLLUP instead of CUBE when you have a year and month column to stop it aggregating the same month across multiple years. I think I understand what this means, but could anyone clarify it? And how do you set it up like this?

Can you use ROLLUP to exclude other combinations of columns as well? My table above is quite simple and the query shows you "pets by store", but if there were other columns, could you include/exclude them from the results?

like image 922
NibblyPig Avatar asked Jul 01 '10 09:07

NibblyPig


People also ask

How does rollup work in SQL?

ROLLUP enables a SELECT statement to calculate multiple levels of subtotals across a specified group of dimensions. It also calculates a grand total. ROLLUP is a simple extension to the GROUP BY clause, so its syntax is extremely easy to use. The ROLLUP extension is highly efficient, adding minimal overhead to a query.

What does GROUP BY rollup do?

GROUP BY ROLLUP is an extension of the GROUP BY clause that produces sub-total rows (in addition to the grouped rows). Sub-total rows are rows that further aggregate whose values are derived by computing the same aggregate functions that were used to produce the grouped rows.

Which aggregate function is used with rollup?

Specifies the type of aggregation to apply to summarized values. These values appear at the higher levels of lists and crosstabs. For OLAP data sources, a rollup aggregate function of Count Distinct is supported for only levels and member sets.


1 Answers

Best explained through an example. Suppose you group by A, B, C. You then get the following groupings with rollup:

(A, B, C)
(A, B)
(A)
()

So you see that the order is important, as you already found out. If you group by A, C, B, you get the following groupings instead:

(A, C, B)
(A, C)
(A)
()
like image 167
Ronald Wildenberg Avatar answered Oct 20 '22 00:10

Ronald Wildenberg