SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

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

CoffeeAddict
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

webfred
Flowing Fount of Yak Knowledge

Germany
8765 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

sql-programmers
Posting Yak Master

USA
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
http://www.sql-programmers.com/

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

visakh16
Very Important crosS Applying yaK Herder

India
52317 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
http://visakhm.blogspot.com/

Go to Top of Page
  Previous Topic Topic Next Topic  
 New 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.08 seconds. Powered By: Snitz Forums 2000