Add a summary row with totals

SqlSql ServerRollup

Sql Problem Overview


I know this sounds crazy and probably should not be done this way but I need something like this - I have a records from SELECT [Type], [Total Sales] From Before

I want to add an extra row at the end to show the SUM at the end of the table (After). Could this be done?

enter image description here

Sql Solutions


Solution 1 - Sql

If you are on SQL Server 2008 or later version, you can use the [ROLLUP()](http://msdn.microsoft.com/en-us/library/bb522495.aspx "Using GROUP BY with ROLLUP, CUBE, and GROUPING SETS") GROUP BY function:

SELECT
  Type = ISNULL(Type, 'Total'),
  TotalSales = SUM(TotalSales)
FROM atable
GROUP BY ROLLUP(Type)
;

This assumes that the Type column cannot have NULLs and so the NULL in this query would indicate the rollup row, the one with the grand total. However, if the Type column can have NULLs of its own, the more proper type of accounting for the total row would be like in @Declan_K's answer, i.e. using the [GROUPING()](http://msdn.microsoft.com/en-us/library/ms178544.aspx "GROUPING (Transact-SQL)") function:

SELECT
  Type = CASE GROUPING(Type) WHEN 1 THEN 'Total' ELSE Type END,
  TotalSales = SUM(TotalSales)
FROM atable
GROUP BY ROLLUP(Type)
;

Solution 2 - Sql

This is the more powerful grouping / rollup syntax you'll want to use in SQL Server 2008+. Always useful to specify the version you're using so we don't have to guess.

SELECT 
  [Type] = COALESCE([Type], 'Total'), 
  [Total Sales] = SUM([Total Sales])
FROM dbo.Before
GROUP BY GROUPING SETS(([Type]),());

Craig Freedman wrote a great blog post introducing GROUPING SETS.

Solution 3 - Sql

Try to use union all as below

SELECT [Type], [Total Sales] From Before
union all
SELECT 'Total', Sum([Total Sales]) From Before

if you have problem with ordering, as i-one suggested try this:

select [Type], [Total Sales] 
from (SELECT [Type], [Total Sales], 0 [Key] 
      From Before 
      union all 
      SELECT 'Total', Sum([Total Sales]), 1 From Before) sq 
order by [Key], Type

Solution 4 - Sql

You could use the ROLLUP operator

SELECT  CASE 
            WHEN (GROUPING([Type]) = 1) THEN 'Total'
            ELSE [Type] END AS [TYPE]
        ,SUM([Total Sales]) as Total_Sales
From    Before
GROUP BY
        [Type] WITH ROLLUP

Solution 5 - Sql

If you want to display more column values without an aggregation function use GROUPING SETS instead of ROLLUP:

SELECT
  Type = ISNULL(Type, 'Total'),
  SomeIntColumn = ISNULL(SomeIntColumn, 0),
  TotalSales = SUM(TotalSales)
FROM atable
GROUP BY GROUPING SETS ((Type, SomeIntColumn ), ())
ORDER BY SomeIntColumn --Displays summary row as the first row in query result

Solution 6 - Sql

Need more specifics with your question but say "type" is from table called letters and "sales" is from a table called transactions. You can use UNION ALL.

SELECT type, COUNT(sales) "total sales" FROM letters GROUP BY type UNION ALL SELECT 'Total', COUNT(sales) "total sales" FROM transactions

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser2103670View Question on Stackoverflow
Solution 1 - SqlAndriy MView Answer on Stackoverflow
Solution 2 - SqlAaron BertrandView Answer on Stackoverflow
Solution 3 - SqlRobertView Answer on Stackoverflow
Solution 4 - SqlDeclan_KView Answer on Stackoverflow
Solution 5 - Sqluser3818229View Answer on Stackoverflow
Solution 6 - SqlVilson SolomonView Answer on Stackoverflow