How to get the Sum of all column values in the last row of a resultset?

前端 未结 3 1195
挽巷
挽巷 2020-12-29 12:24

I need to get the sum of all column values of a result set in the last row.
Here is my SQL query.

select Master_Code, SUM(Jan), SUM(Feb), SUM(Mar)
from d         


        
3条回答
  •  梦毁少年i
    2020-12-29 12:39

    Make a union where you repeat the same query but without the grouping:

    select Title, Jan, Feb, Mar
    from (
      select Master_Code as Title, SUM(Jan) as Jan, SUM(Feb) as Feb, SUM(Mar) as Mar
      from dbo.foobar
      WHERE Participating_City = 'foofoo'
      GROUP BY Master_Code ORDER BY Master_Code ASC
    ) x
    union all
    select 'Total', SUM(Jan) as Jan, SUM(Feb) as Feb, SUM(Mar) as Mar
    from dbo.foobar
    WHERE Participating_City = 'foofoo'
    

提交回复
热议问题