SQL Query To Sum Two Columns Values Based on Its Record
May i know what is the SQL query to create another intermediate column which is used to store the sum of two columns of each and every record. Thank you.
March 27th, 2007 8:02am
Maybe this is not what you want, can you please describe more about your requirements?
SELECT column1, column2, (column1+column2) AS SumColumn FROM Table1
Shyam
Free Windows Admin Tool Kit Click here and download it now
March 27th, 2007 8:22am
Thanks for your reply, it is what i want but the output is slightly different from my expectation. cuz the new column called SumColumn displayed the sums of two zero values as 00and non zero values as 03.4, may i know how to get rid of the first zero. Thank you.
March 28th, 2007 5:37am
Are the columns strings? If so, you need to cast them to numbers before adding them.
Free Windows Admin Tool Kit Click here and download it now
March 28th, 2007 6:43am
you can convert to decimal like this:
CONVERT(DECIMAL(9, 2), Column1+Column2) AS SumColumn
Shyam
Pleasemark the post as answer.
March 28th, 2007 8:49am
Or you can format the field in your report like this:
Format(Fields!SumColumn.Value, "#.0")
Shyam
Free Windows Admin Tool Kit Click here and download it now
March 29th, 2007 3:10pm
Thanks for solution for sum of two column.
April 30th, 2010 10:43am
Thanks for sum i used it like this may be helpfull to other fellows as well.
SELECT distinct(dbo.table1.ProductNum),
(cast(Column1 as int) + cast(Column2 as int) + cast(Column3 as int) + cast(Column4 as int) + cast(Column5 as int) + cast(Column6 as int) + cast(Column7 as int) + cast(Column8 as int))
as sumofcolumns
from Table2 Inner join dbo.Table1 on dbo.Table2.ProductNum=dbo.Table1.ProductNum
where (dbo.Table2.Datestamp between CONVERT(datetime, '2010-09-01 06:00:00',102)and CONVERT(datetime, '2010-09-01 18:00:00',102))
________________
Eric
Ericcisco
Free Windows Admin Tool Kit Click here and download it now
November 11th, 2010 4:46am
in ms sql if want to create space between columns +' '+ sorry for missing before Eric
December 1st, 2010 4:56am