2012-08-01 77 views
0

我使用用戶Keevas'的例子,但問不同的問題的最後一列....SQL-如何獲得和列中的每個總和列

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 

如何獲得總價值欄?謝謝!

+0

您使用的是什麼RDBMS? – Taryn 2012-08-01 16:15:45

回答

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 
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 
0

使用加號只需添加他們。

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