在Sql Server中汇总列

时间:2014-05-26 07:57:32

标签: sql-server-2008

我有一个sql server表,如下所示:

c1  c2  c3
X   30.00   0.00
X   15.00   0.00
Y   0.00    20.00

你能帮助我获得:

c1  c2       c3      c4 
Z   45.00   20.00   65

到目前为止我尝试了什么:

select 'Z' AS C1, SUM(c2) as c2, SUM(c3) AS C3, SUM(c3) + SUM(c2) AS c4 
  from Table1
  group by c1

给了我这个结果:

C1  c2      C3       c4
Z   45.00   0.00    45.00
Z   0.00    20.00   20.00

而我希望所有的总和在一条线上。 谢谢!

1 个答案:

答案 0 :(得分:1)

SELECT 'Z' AS c1, SUM(c2) AS c2, SUM(c3) AS c3, SUM(c3) + SUM(c2) AS c4   
  FROM Table1

输出:

c1 c2 c3 c4             
Z  45 20 65