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 : 3361 | Status : [Member] | Replies : 3

Suppose i have a table having two columns


output

col1 col2 sum

1 2 3

3 4 7


5 6 11





Responses

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