Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

summing values that can be a null or float value

Posted on 2011-03-03
15
Medium Priority
?
198 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

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!

Question has a verified solution.

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

I am showing a way to read/import the excel data in table using SQL server 2005... Suppose there is an Excel file "Book1" at location "C:\temp" with column "First Name" and "Last Name". Now to import this Excel data into the table, we will use…
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.
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

610 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