Search code examples
sqlsql-serversql-server-2005

How to find sum of multiple columns in a table in SQL Server 2005?


I have a table Emp which has these rows:

Emp_cd | Val1  | Val2  | Val3  | Total
-------+-------+-------+-------+-------
 1     | 1.23  | 2.23  | 3.43  | 
 2     | 23.03 | 12.23 | 2.92  |
 3     | 7.23  | 9.05  | 13.43 |
 4     | 03.21 | 78.23 | 9.43  |

I want to find SUM of Val1, Val2, Val3 and which will show in the Total column.


Solution

  • Easy:

    SELECT 
       Val1,
       Val2,
       Val3,
       (Val1 + Val2 + Val3) as 'Total'
    FROM Emp
    

    or if you just want one row:

    SELECT 
       SUM(Val1) as 'Val1',
       SUM(Val2) as 'Val2',
       SUM(Val3) as 'Val3',
       (SUM(Val1) + SUM(Val2) + SUM(Val3)) as 'Total'
    FROM Emp