user3652812
user3652812

Reputation: 101

Summing columns in Sql Server

I have an sql server table as follows:

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

Can you please help me obtain:

c1  c2       c3      c4 
Z   45.00   20.00   65

What I tried so far:

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

gives me this result:

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

whereas I want all the sums on one single line. Thanks!

Upvotes: 0

Views: 75

Answers (1)

TheCrafter
TheCrafter

Reputation: 1939

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

Output:

c1 c2 c3 c4             
Z  45 20 65

Upvotes: 1

Related Questions