Add a summary row with totals

后端 未结 5 1593
不知归路
不知归路 2020-11-27 02:55

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

5条回答
  •  [愿得一人]
    2020-11-27 03:44

    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.

提交回复
热议问题