Skip to content

CUBE

SELECT Country, Year, SUM(Sales) AS TotalSales
FROM SalesData
GROUP BY CUBE (Country, Year);
CountryYearTotalSales
USA2018100000
USA2019120000
UK201890000
UK2019105000
NULL2018190000
NULL2019225000
USANULL220000
UKNULL195000
NULLNULL415000

In this query, CUBE is used to generate a result set that represents aggregates for all combinations of values in the selected columns. It produces the totals by Country, by Year, total for all Countries per year, total for all years per Country, and overall total. NULL represents the total across that column.