Solved

summing values that can be a null or float value

Posted on 2011-03-03
15
190 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
Comment Utility
SUM(ISNULL(U_CPA_NUMBER, 0)) AS U_CPA_NUMBER
0
 
LVL 13

Assisted Solution

by:Philip Pinnell
Philip Pinnell earned 250 total points
Comment Utility
try

sum(isnull(U_CPA_NUMER,0))
0
 

Author Comment

by:wilcon
Comment Utility
tried that as well still comes back with 0
0
 
LVL 13

Expert Comment

by:Philip Pinnell
Comment Utility
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
Comment Utility
for me both sums return 9
0
 

Author Comment

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

Expert Comment

by:Philip Pinnell
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:wilcon
Comment Utility
Yes this case works.
0
 
LVL 13

Expert Comment

by:Philip Pinnell
Comment Utility
0
 
LVL 13

Expert Comment

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

Expert Comment

by:Philip Pinnell
Comment Utility
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
Comment Utility
There is definitely NULL and Float values.
0
 
LVL 13

Expert Comment

by:Philip Pinnell
Comment Utility
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
Comment Utility
found the issue thanks!
0
 
LVL 13

Expert Comment

by:Philip Pinnell
Comment Utility
...and it was?
0

Featured Post

Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

Join & Write a Comment

If you having speed problem in loading SQL Server Management Studio, try to uncheck these options in your internet browser (IE -> Internet Options / Advanced / Security):    . Check for publisher's certificate revocation    . Check for server ce…
In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

772 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