Alright, let's wrap things up!
GROUP BY ROLLUP() works by creating additional rows with fewer grouping columns, as shown below:
GROUP BY ROLLUP (A, B, C) =
GROUP BY (A, B, C) +
GROUP BY (A, B) +
GROUP BY (A) +
GROUP BY ()
- By changing the order of columns inside
ROLLUP, we change the grouping levels created.
- Not all columns must be included inside
ROLLUP. Those outside its parentheses will always be used for grouping.
- We can use
COALESCE(column_name, substitute_value) to show the
GROUPING(column_name) function shows if the
column_name column is used in the grouping.
Now, how about a short quiz?