The short answer
CUBE generates aggregates for all combinations of the grouping columns.
For 3 columns:
2³ = 8So CUBE(a,b,c) produces up to 8 grouping combinations.
For example:
GROUP BY CUBE(country, city, product)can produce totals for:
- country + city + product
- country + city
- country + product
- city + product
- country
- city
- product
- grand total