SlideShare a Scribd company logo
1 of 7
Use SQL ROLLUP to generate
sub totals and grand totals
Did you know you can easily
add sub totals and grand totals
to your aggregated queries
In this example we are using a
date dimension table to return
the number of days per
calendar month
SELECT [Year], [quarter], [monthname],
count([date]) as [no of days]
FROM [dbo].[DimDate]
WHERE [Year] = 2022
GROUP BY [Year], [quarter], [monthname]
ROLLUP is an extension to the
group by clause
By default it adds a total row to
the columns you specify
SELECT [Year], [quarter], [monthname],
count([date]) as [no of days]
FROM [dbo].[DimDate]
WHERE [Year] = 2022
GROUP BY ROLLUP ([Year], [quarter], [monthname])
Totals and Sub Totals by
default are labelled as NULL
It is worth changing these with
a bit of logic
We can use coalesce to give
these totals a specific name
SELECT coalesce ([Year],'Total All Years') as [Year]
, coalesce ([quarter],'Total All Quarters') as [Quarter]
, coalesce ([monthname], 'Total All Months') as [Month]
, count([date]) as [no of days]
FROM [dbo].[DimDate]
WHERE [Year] = 2022
GROUP BY ROLLUP ([Year], [quarter], [monthname])
This technique is really powerful and can
be added to group by queries with little
effort
It can be used with other aggregation
functions (SUM, COUNT, AVG, MAX, MIN)
Partial rollups can also be used to avoid
certain sub totals in this example
SELECT coalesce ([Year],'Total All Years') as [Year]
, coalesce ([quarter],'Total All Quarters') as [Quarter]
, coalesce ([monthname], 'Total All Months') as [Month]
, count([date]) as [no of days]
FROM [dbo].[DimDate]
WHERE [Year] = 2022
GROUP BY [Year], ROLLUP ([quarter], [monthname])
Hope you found this useful
For more Tips, Tricks and
Timesavers, visit our website
Tips and Timesavers | Select Distinct Limited
Credit: simon.harrison@selectdistinct.co.uk

More Related Content

More from Select Distinct Limited

Calculated Columns and Measures in Power BI.pptx
Calculated Columns and Measures in Power BI.pptxCalculated Columns and Measures in Power BI.pptx
Calculated Columns and Measures in Power BI.pptxSelect Distinct Limited
 
When to transform data for Power BI.pptx
When to transform data for Power BI.pptxWhen to transform data for Power BI.pptx
When to transform data for Power BI.pptxSelect Distinct Limited
 
Direction of travel on a map in Power BI.pptx
Direction of travel on a map in Power BI.pptxDirection of travel on a map in Power BI.pptx
Direction of travel on a map in Power BI.pptxSelect Distinct Limited
 
Power BI Connect to Google BigQuery.pptx
Power BI Connect to Google BigQuery.pptxPower BI Connect to Google BigQuery.pptx
Power BI Connect to Google BigQuery.pptxSelect Distinct Limited
 
How to use a slicer to toggle measures in Power BI
How to use a slicer to toggle measures in Power BIHow to use a slicer to toggle measures in Power BI
How to use a slicer to toggle measures in Power BISelect Distinct Limited
 
Power BI Tips Rolling Averages and Rolling Sums.pptx
Power BI Tips Rolling Averages and Rolling Sums.pptxPower BI Tips Rolling Averages and Rolling Sums.pptx
Power BI Tips Rolling Averages and Rolling Sums.pptxSelect Distinct Limited
 
Power BI Templates and how to use them.pptx
Power BI Templates and how to use them.pptxPower BI Templates and how to use them.pptx
Power BI Templates and how to use them.pptxSelect Distinct Limited
 

More from Select Distinct Limited (20)

Calculated Columns and Measures in Power BI.pptx
Calculated Columns and Measures in Power BI.pptxCalculated Columns and Measures in Power BI.pptx
Calculated Columns and Measures in Power BI.pptx
 
Divide by Zero Errors
Divide by Zero ErrorsDivide by Zero Errors
Divide by Zero Errors
 
When to transform data for Power BI.pptx
When to transform data for Power BI.pptxWhen to transform data for Power BI.pptx
When to transform data for Power BI.pptx
 
Power BI KPIs
Power BI KPIsPower BI KPIs
Power BI KPIs
 
Direction of travel on a map in Power BI.pptx
Direction of travel on a map in Power BI.pptxDirection of travel on a map in Power BI.pptx
Direction of travel on a map in Power BI.pptx
 
UNION in DAX
UNION in DAXUNION in DAX
UNION in DAX
 
APPEND data in Power Query
APPEND data in Power QueryAPPEND data in Power Query
APPEND data in Power Query
 
Power BI Connect to Google BigQuery.pptx
Power BI Connect to Google BigQuery.pptxPower BI Connect to Google BigQuery.pptx
Power BI Connect to Google BigQuery.pptx
 
Group by ROLLUP in SQL Server
Group by ROLLUP in SQL ServerGroup by ROLLUP in SQL Server
Group by ROLLUP in SQL Server
 
Advanced Top N Measures in Power BI
Advanced Top N Measures in Power BIAdvanced Top N Measures in Power BI
Advanced Top N Measures in Power BI
 
Custom Formats in Power BI
Custom Formats in Power BICustom Formats in Power BI
Custom Formats in Power BI
 
Power BI Tips Top N Measures.pptx
Power BI Tips Top N Measures.pptxPower BI Tips Top N Measures.pptx
Power BI Tips Top N Measures.pptx
 
SQL Tips UNPIVOT Function.pptx
SQL Tips UNPIVOT Function.pptxSQL Tips UNPIVOT Function.pptx
SQL Tips UNPIVOT Function.pptx
 
Power BI Drill Through
Power BI Drill ThroughPower BI Drill Through
Power BI Drill Through
 
How to PIVOT in SQL
How to PIVOT in SQLHow to PIVOT in SQL
How to PIVOT in SQL
 
How to use a slicer to toggle measures in Power BI
How to use a slicer to toggle measures in Power BIHow to use a slicer to toggle measures in Power BI
How to use a slicer to toggle measures in Power BI
 
Power BI Tips Rolling Averages and Rolling Sums.pptx
Power BI Tips Rolling Averages and Rolling Sums.pptxPower BI Tips Rolling Averages and Rolling Sums.pptx
Power BI Tips Rolling Averages and Rolling Sums.pptx
 
Power BI Templates and how to use them.pptx
Power BI Templates and how to use them.pptxPower BI Templates and how to use them.pptx
Power BI Templates and how to use them.pptx
 
How to use Format painter in Excel
How to use Format painter in ExcelHow to use Format painter in Excel
How to use Format painter in Excel
 
SQL Select Distinct Statement
SQL Select Distinct StatementSQL Select Distinct Statement
SQL Select Distinct Statement
 

SQL Tips ROLLUP.pptx

  • 1. Use SQL ROLLUP to generate sub totals and grand totals
  • 2. Did you know you can easily add sub totals and grand totals to your aggregated queries In this example we are using a date dimension table to return the number of days per calendar month SELECT [Year], [quarter], [monthname], count([date]) as [no of days] FROM [dbo].[DimDate] WHERE [Year] = 2022 GROUP BY [Year], [quarter], [monthname]
  • 3. ROLLUP is an extension to the group by clause By default it adds a total row to the columns you specify SELECT [Year], [quarter], [monthname], count([date]) as [no of days] FROM [dbo].[DimDate] WHERE [Year] = 2022 GROUP BY ROLLUP ([Year], [quarter], [monthname])
  • 4. Totals and Sub Totals by default are labelled as NULL It is worth changing these with a bit of logic We can use coalesce to give these totals a specific name SELECT coalesce ([Year],'Total All Years') as [Year] , coalesce ([quarter],'Total All Quarters') as [Quarter] , coalesce ([monthname], 'Total All Months') as [Month] , count([date]) as [no of days] FROM [dbo].[DimDate] WHERE [Year] = 2022 GROUP BY ROLLUP ([Year], [quarter], [monthname])
  • 5. This technique is really powerful and can be added to group by queries with little effort It can be used with other aggregation functions (SUM, COUNT, AVG, MAX, MIN) Partial rollups can also be used to avoid certain sub totals in this example SELECT coalesce ([Year],'Total All Years') as [Year] , coalesce ([quarter],'Total All Quarters') as [Quarter] , coalesce ([monthname], 'Total All Months') as [Month] , count([date]) as [no of days] FROM [dbo].[DimDate] WHERE [Year] = 2022 GROUP BY [Year], ROLLUP ([quarter], [monthname])
  • 6. Hope you found this useful
  • 7. For more Tips, Tricks and Timesavers, visit our website Tips and Timesavers | Select Distinct Limited Credit: simon.harrison@selectdistinct.co.uk