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

前端 未结 3 1193
挽巷
挽巷 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条回答
  •  温柔的废话
    2020-12-29 12:57

    Assuming there are no null master_code rows.

    SELECT ISNULL(Master_code, 'Total') AS Master_Code,
           Jan,
           Feb,
           Mar
    FROM (
          SELECT Master_code,
                 SUM(Jan) AS Jan,
                 SUM(Feb) AS Feb,
                 SUM(Mar) AS Mar
          FROM foobar
          WHERE Participating_City = 'foofoo'
          GROUP BY Master_code WITH ROLLUP
         ) AS DT
    

提交回复
热议问题