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

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Summing 2 fields Question
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Yak Posting Veteran

94 Posts

Posted - 09/26/2012 :  23:09:22  Show Profile  Reply with Quote
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)

Edited by - CoffeeAddict on 09/27/2012 00:10:43

Flowing Fount of Yak Knowledge

8781 Posts

Posted - 09/27/2012 :  02:52:07  Show Profile  Visit webfred's Homepage  Reply with Quote
can you post the error message please?

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

Posting Yak Master

190 Posts

Posted - 09/27/2012 :  06:00:55  Show Profile  Visit sql-programmers's Homepage  Reply with Quote
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

Edited by - sql-programmers on 09/27/2012 06:02:58
Go to Top of Page

Very Important crosS Applying yaK Herder

52326 Posts

Posted - 09/27/2012 :  10:32:24  Show Profile  Reply with Quote
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

Go to Top of Page
  Previous Topic Topic Next Topic  
 Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.02 seconds. Powered By: Snitz Forums 2000