Solved

summing values that can be a null or float value

Posted on 2011-03-03
15
191 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
  • 9
  • 5
15 Comments
 
LVL 9

Accepted Solution

by:
joshbula earned 250 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 250 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
 
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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 

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

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

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

Suggested Solutions

There are some very powerful Data Management Views (DMV's) introduced with SQL 2005. The two in particular that we are going to discuss are sys.dm_db_index_usage_stats and sys.dm_db_index_operational_stats.   Recently, I was involved in a discuā€¦
Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based developeā€¦
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.

895 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now