If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > Sum() statement is not working correctly.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-19-04, 17:11
frankyvalley frankyvalley is offline
Registered User
 
Join Date: Feb 2004
Posts: 5
Sum() statement is not working correctly.

if I perform the query:
select stolen, damaged from products where prodid = '343'
it returns 2 fields, one contains 50 and the other contains nothing (null)

If I then perform the querry:
select sum(stolen) + sum(damaged) as lost from products where prodid = '343'

It returns a null value, any ideas why?

is it because the one value is null?


thanks
Reply With Quote
  #2 (permalink)  
Old 02-19-04, 17:43
ss659 ss659 is offline
Registered User
 
Join Date: Jan 2004
Posts: 492
Re: Sum() statement is not working correctly.

Quote:
Originally posted by frankyvalley
if I perform the query:
select stolen, damaged from products where prodid = '343'
it returns 2 fields, one contains 50 and the other contains nothing (null)

If I then perform the querry:
select sum(stolen) + sum(damaged) as lost from products where prodid = '343'

It returns a null value, any ideas why?

is it because the one value is null?


thanks
Because null is not a value - It is not like the value 0. So anything + null always evaluates to NULL. If this is on oracle, do something like this:

Code:
Select nvl(sum(stolen),0) + nvl(sum(stolen),0) as lost 
from products
where prodid = '343'
I believe other databases use ISNULL.

Hope this helps.
Reply With Quote
  #3 (permalink)  
Old 02-20-04, 10:29
frankyvalley frankyvalley is offline
Registered User
 
Join Date: Feb 2004
Posts: 5
Thats exactly what it was. I was thinking that adding to null was causing a problem but I wasn't too sure how to correct it!


thanks for all your help!!!!!
Reply With Quote
  #4 (permalink)  
Old 02-20-04, 11:07
frankyvalley frankyvalley is offline
Registered User
 
Join Date: Feb 2004
Posts: 5
Thats exactly what it was. I was thinking that adding to null was causing a problem but I wasn't too sure how to correct it!


thanks for all your help!!!!!
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On