user3197575
user3197575

Reputation: 277

How to sum columns in SQL Server?

I am trying to create a column that is the total value of column A,B,C,D,E.

 select,[TVIncome] a
  ,[XIncome] b
  ,[ZIncome] c
  ,[DINCOME] d
  ,[OIncome] e

sum(a,b,c,d,e) as total

The error I get when doing the above sum is :"The sum function requires 1 argument(s)."

The total above does not work. Also, if I just include the the proper names, XIncome and the rest,it still does not work. How do I do it?

Upvotes: 0

Views: 4402

Answers (2)

Caleth
Caleth

Reputation: 63152

SUM() adds each value in a column, to give a single value + adds each value in two columns together to give a column

It sounds like you are after a + b + c + d + e AS total, or possibly SUM(a + b + c + d + e) AS total if you are after 1 value

Upvotes: 2

Tom
Tom

Reputation: 7740

Assuming you want a sum for those columns for each row

SELECT (TVIncome + XIncome + ZIncome + DIncome + OIncome) as TotalIncome
FROM Table

If you want a sum of sums, which will equal one row:

SELECT SUM(TVIncome + XIncome + ZIncome + DIncome  + OIncome) as TotalIncome
FROM Table

Upvotes: 2

Related Questions