Skip to content
Advertisement

SQL ROLLUP with COALESCE

The data:

The query:

The output I get is like this:

enter image description here

It gives null every individual brand item not grouped, and I want the last null to be ‘Grand Total’ not ‘Monthly Total’. I tried putting PurchaseDate or Brand column in and out the ROLLUP but it doesn’t seem to work for the Brand to be grouped.

My desired output would be this. Compute all the item purchased per brand and per month, as well as the grand total of all purchased items in all date which I mentioned earlier

enter image description here

Advertisement

Answer

This could be done using CASE WHEN structure with GROUPING as follows:

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement