Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Summing 2 fields Question

Author  Topic 

CoffeeAddict
Yak Posting Veteran

94 Posts

Posted - 2012-09-26 : 23:09:22
I'm trying to sum these 2 fields up. I don't know if I should do just Sum(firstField) + Sum(SecondField) or Sum(FirstField + SecondField) but I can't get it to work regardless due to syntax errors:

select CarCondemned.CarKey,
SUM(CarCondemned.Head + carsheet.UserFld2) AS AUPU
from CarCondemned CarCondemned
join CarSheet carsheet on carsheet.CarKey = CarCondemned.CarKey
where CarCondemned.CauseCode = 80
group by CarCondemned.CarKey


Head is type int and UserFld2 is type decimal(13,5)

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2012-09-27 : 02:52:07
can you post the error message please?


Too old to Rock'n'Roll too young to die.
Go to Top of Page

sql-programmers
Posting Yak Master

190 Posts

Posted - 2012-09-27 : 06:00:55
Sum(firstField) + Sum(SecondField) or Sum(FirstField + SecondField)

Both result will be same we don't bother about that But If any one filed is NULLABLE in BOTH fields, We need to write query like

ISNULL(Sum(firstField), 0) + ISNULL(Sum(SecondField), 0) or Sum(ISNULL(FirstField, 0) + ISNULL(SecondField, 0))

In the both method , I think Sum(ISNULL(FirstField, 0) + ISNULL(SecondField, 0)) method more sufficient.

Can you post error message?

I think your query is write. but i dont know why it gives error.

Solution: change your alias name of the Table and execute the query

SQL Server Programmers and Consultants
http://www.sql-programmers.com/
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-09-27 : 10:32:24
Op has not shown full query so it might be that error is caused by some other portion. But for that we atleast need to see the error message

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -