Display sum of two columns of a table using sql query [Resolved]

Posted by Kundan under Sql Server on 7/31/2013 | Points: 10 | Views : 2619 | Status : [Member] | Replies : 4

Suppose i have a table having two columns


output

col1 col2 sum

1 2 3

3 4 7


5 6 11





Responses

Posted by: Satyapriyanayak on: 7/31/2013 [Member] [MVP] Silver | Points: 50

Up
0
Down

Resolved
Make sure column datatype as int


SELECT col1, col2, col1 + col2 AS 'Sum'
FROM Table1

If this post helps you mark it as answer
Thanks

Kundan, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ssj_Kumar on: 7/31/2013 [Member] Starter | Points: 50

Up
0
Down

Resolved
select col1,col2,col1+col2 as Total from Table

Regards,
Jayakumar Selvakani

Kundan, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: aswinialuri-19361 on: 7/31/2013 [Member] Starter | Points: 50

Up
0
Down

Resolved
Hi,
select col1,col2 ,(col1+col2) as Total
if you get any doubt about this refer this link it will help you
http://stackoverflow.com/questions/11976792/how-to-sum-two-columns-at-left-and-display-total-in-third-column
http://stackoverflow.com/questions/11033340/how-to-find-sum-of-multiple-columns-in-a-table-in-sql-server-2005

Mark as Answer if it helps you
Thanks&Regards
Aswini Aluri

Kundan, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 8/1/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Make sure to handle the Nullable columns also....
col1  col2  sum


1 NULL 1

3 4 7


5 6 11


In the case of Null values we must replace Null with zero for addition/subtractions; else we will get incorrect results...
SELECT col1, col2, ISNULL(col1, 0)+ISNULL(col2, 0) Sum 

FROM TableName

Mark it as answer if it helps you......


Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Kundan, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response