?
Solved

summing values that can be a null or float value

Posted on 2011-03-03
15
Medium Priority
?
197 Views
Last Modified: 2012-06-27
I have a query where I sum a value sum(U_CPA_NUMER), this value can be a NULL or Float values.  If a null is one of the values my result is null. I have tried using a case statement,

sum(case when u_cpa_numer is null then 0 else u_cpa_numer end) but this yields a 0 value only.

I tried excluding null values for this field in my where clause but this wacks out the whole query.  Please help.
0
Comment
Question by:wilcon
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 5
15 Comments
 
LVL 9

Accepted Solution

by:
joshbula earned 1000 total points
ID: 35027740
SUM(ISNULL(U_CPA_NUMBER, 0)) AS U_CPA_NUMBER
0
 
LVL 13

Assisted Solution

by:Philip Pinnell
Philip Pinnell earned 1000 total points
ID: 35027743
try

sum(isnull(U_CPA_NUMER,0))
0
 

Author Comment

by:wilcon
ID: 35027760
tried that as well still comes back with 0
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 13

Expert Comment

by:Philip Pinnell
ID: 35027820
Interesting

try this

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[test]') AND type in (N'U'))
DROP TABLE [dbo].[test]
go
CREATE TABLE [dbo].[test](
      [id] [smallint] IDENTITY(1,1) NOT NULL,
      [val] [float] NULL
) ON [PRIMARY]


INSERT INTO [test]
           ([val])
     VALUES
           (2)
INSERT INTO [test]
           ([val])
     VALUES
           (3)
INSERT INTO [test]
           ([val])
     VALUES
           (Null)
INSERT INTO [test]
           ([val])
     VALUES
           (4)
INSERT INTO [test]
           ([val])
     VALUES
           (Null)

SELECT * from [test]
SELECT
sum([val]) as s1,
sum(isnull([val],0)) as s2
  FROM [test]
0
 
LVL 13

Expert Comment

by:Philip Pinnell
ID: 35027824
for me both sums return 9
0
 

Author Comment

by:wilcon
ID: 35027957
is it possible it's because my values are of float datatype
0
 
LVL 13

Expert Comment

by:Philip Pinnell
ID: 35027998
The example above is a float datatype.

When you run the above SQL do you get 9 for both sums?

I presume the correct sum for query is definitely not 0
0
 

Author Comment

by:wilcon
ID: 35028052
Yes this case works.
0
 
LVL 13

Expert Comment

by:Philip Pinnell
ID: 35028071
0
 
LVL 13

Expert Comment

by:Philip Pinnell
ID: 35028077
can you post some example data from your table and the schema?
0
 
LVL 13

Expert Comment

by:Philip Pinnell
ID: 35028109
If your where query only includes null values however the result will be null and the isnull(U_CPA_NUMER,0) version will be 0

This seems to describe your situation.

Please check the data or try with different criteria
0
 

Author Comment

by:wilcon
ID: 35028136
There is definitely NULL and Float values.
0
 
LVL 13

Expert Comment

by:Philip Pinnell
ID: 35028174
If you do

select U_CPA_NUMER
from your table
where yourwhereclause

instead of

select sum(U_CPA_NUMER)
from your table
where yourwhereclause

You do get none 0 values as well as nulls?
0
 

Author Comment

by:wilcon
ID: 35028198
found the issue thanks!
0
 
LVL 13

Expert Comment

by:Philip Pinnell
ID: 35028208
...and it was?
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

by Mark Wills PIVOT is a great facility and solves many an EAV (Entity - Attribute - Value) type transformation where we need the information held as data within a column to become columns in their own right. Now, in some cases that is relatively…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question