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

SqlSql ServerSql Server-2005

Sql Problem Overview


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.

Sql Solutions


Solution 1 - Sql

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

Solution 2 - Sql

You must also be aware of null records:

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

Usage of ISNULL:

ISNULL(col_Name, replace value)

Solution 3 - Sql

Just as a regular SELECT?

SELECT 
   Val1, Val2, Val3,
   Total = Val1 + Val2 + Val3
FROM dbo.Emp

Or do you want to determine that total and update the table with those values?

UPDATE dbo.Emp
SET Total = Val1 + Val2 + Val3

If you want to have this total be current at all times - you should have a computed column in your table:

ALTER TABLE dbo.Emp
ADD CurrentTotal AS Val1 + Val2 + Val3 PERSISTED

Then you will always get the current total - even if the values change:

SELECT 
   Val1, Val2, Val3, CurrentTotal
FROM dbo.Emp

Solution 4 - Sql

Try this:

select sum(num_tax_amount+num_total_amount) from table_name;

Solution 5 - Sql

Another example using COALESCE. http://sqlmag.com/t-sql/coalesce-vs-isnull

SELECT (COALESCE(SUM(val1),0) + COALESCE(SUM(val2), 0)
+ COALESCE(SUM(val3), 0) + COALESCE(SUM(val4), 0)) AS 'TOTAL'
FROM Emp

Solution 6 - Sql

Hi You can use a simple query,

select emp_cd, val1, val2, val3,
(val1+val2+val3) as total 
from emp;

In case you need to insert a new row,

insert into emp select emp_cd, val1, val2, val3,
(val1+val2+val3) as total 
from emp;

In order to update,

update emp set total = val1+val2+val3;

This will update for all comumns

Solution 7 - Sql

Needed to do similar work on a Postgres db table with null value. Ended up creating a function as there are too COALESCE to add.

CREATE OR REPLACE FUNCTION array_sum_float(float[])
	RETURNS float
AS
$$
DECLARE
	arrInts ALIAS FOR $1;
	sum int DEFAULT 0;
BEGIN
	FOR I IN ARRAY_LOWER(arrInts, 1)..ARRAY_UPPER(arrInts, 1)
		LOOP
			sum = sum + COALESCE(arrInts[I], 0);
		END LOOP;
	RETURN sum;
END;
$$
	LANGUAGE plpgsql;

SELECT array_sum_float(ARRAY [6,8, null, 2]);
SELECT array_sum_float(ARRAY [Val1, Val2, Val3]) from Emp;

Solution 8 - Sql

SELECT Emp_cd, Val1, Val2, Val3, SUM(Val1 + Val2 + Val3) AS TOTAL 
FROM Emp
GROUP BY Emp_cd, Val1, Val2, Val3

Solution 9 - Sql

use a trigges it will work:-

->CREATE TRIGGER trigger_name BEFORE INSERT ON table_name

FOR EACH ROW SET NEW.column_name3 = NEW.column_name1 + NEW.column_name2;

this will only work only when you will insert a row in table not when you will be updating your table for such a pupose create another trigger of different name and use UPDATE on the place of INSERT in the above syntax

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTripati SubudhiView Question on Stackoverflow
Solution 1 - SqlaF.View Answer on Stackoverflow
Solution 2 - SqlSalem AhmedView Answer on Stackoverflow
Solution 3 - Sqlmarc_sView Answer on Stackoverflow
Solution 4 - SqlKamranView Answer on Stackoverflow
Solution 5 - SqlDiogo RodriguesView Answer on Stackoverflow
Solution 6 - SqlJim MacaulayView Answer on Stackoverflow
Solution 7 - SqlKuNView Answer on Stackoverflow
Solution 8 - SqlRakesh AnandView Answer on Stackoverflow
Solution 9 - SqlYogeshView Answer on Stackoverflow