SQL-如何在每个和列的最后一列中获取sum列

时间:2012-08-01 16:10:39

标签: sql

我正在使用用户Keevas的例子,但问了一个不同的问题......

select Master_Code, SUM(Jan), SUM(Feb), SUM(Mar)
 from dbo.foobar
WHERE Participating_City = 'foofoo'
GROUP BY Master_Code ORDER BY Master_Code ASC

类似的东西:

Master_Code sum(Jan) Sum(Feb) sum(Mar)  Total
        1     4         5       4        13
        2     5         5       5        15

如何获得总价值列?谢谢!

3 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

这应该有效:

select Master_Code
    , SUM(Jan)
    , SUM(Feb)
    , SUM(Mar)
    , (SUM(Jan + Feb + Mar)) As Total
 from dbo.foobar
WHERE Participating_City = 'foofoo'
GROUP BY Master_Code 
ORDER BY Master_Code ASC

答案 2 :(得分:0)

只需使用加号添加它们。

SELECT Master_Code, SUM(Jan), SUM(Feb), SUM(Mar), SUM(Jan) + SUM(Feb) + SUM(Mar) FROM...