dbTalk Databases Forums  

oracle sum display zero instead of NULL

comp.databases.oracle.misc comp.databases.oracle.misc


Discuss oracle sum display zero instead of NULL in the comp.databases.oracle.misc forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
trpost@gmail.com
 
Posts: n/a

Default oracle sum display zero instead of NULL - 11-01-2007 , 12:55 PM






I have a simple query:

SELECT SUM(cases_escalated) AS casesescalated FROM groupsummary WHERE
report_date=TO_DATE('Sep 2007','MON YYYY') AND group_name = 'T1';

The query works fine, except for when there is nothing to SUM and then
I get returned NULL instead of 0.

My question is how can I get the query to display 0 instead of
'blank'; I read something about using a join, but I don't see how
joining would help me as right now this is the only table in my
database. Would I somehow join against a query to DUAL?


Reply With Quote
  #2  
Old   
fitzjarrell@cox.net
 
Posts: n/a

Default Re: oracle sum display zero instead of NULL - 11-01-2007 , 01:06 PM






On Nov 1, 1:55 pm, trp... (AT) gmail (DOT) com wrote:
Quote:
I have a simple query:

SELECT SUM(cases_escalated) AS casesescalated FROM groupsummary WHERE
report_date=TO_DATE('Sep 2007','MON YYYY') AND group_name = 'T1';

The query works fine, except for when there is nothing to SUM and then
I get returned NULL instead of 0.

My question is how can I get the query to display 0 instead of
'blank'; I read something about using a join, but I don't see how
joining would help me as right now this is the only table in my
database. Would I somehow join against a query to DUAL?

SELECT SUM(nvl(cases_escalated, 0)) AS casesescalated
FROM groupsummary
WHERE report_date=TO_DATE('Sep 2007','MON YYYY')
AND group_name = 'T1';



David Fitzjarrell



Reply With Quote
  #3  
Old   
Frank van Bortel
 
Posts: n/a

Default Re: oracle sum display zero instead of NULL - 11-01-2007 , 01:09 PM



trpost (AT) gmail (DOT) com wrote:
Quote:
I have a simple query:

SELECT SUM(cases_escalated) AS casesescalated FROM groupsummary WHERE
report_date=TO_DATE('Sep 2007','MON YYYY') AND group_name = 'T1';

The query works fine, except for when there is nothing to SUM and then
I get returned NULL instead of 0.

My question is how can I get the query to display 0 instead of
'blank'; I read something about using a join, but I don't see how
joining would help me as right now this is the only table in my
database. Would I somehow join against a query to DUAL?

By not summing NULLs: sum(nvl(cases_escalated,0)),
or by decoding the NULL: nvl(sum(cases_escalated),0)

Which ever way you want it
--
Regards,
Frank van Bortel

Top-posting is one way to shut me up...


Reply With Quote
  #4  
Old   
Maxim Demenko
 
Posts: n/a

Default Re: oracle sum display zero instead of NULL - 11-01-2007 , 03:04 PM



fitzjarrell (AT) cox (DOT) net schrieb:
Quote:
On Nov 1, 1:55 pm, trp... (AT) gmail (DOT) com wrote:
I have a simple query:

SELECT SUM(cases_escalated) AS casesescalated FROM groupsummary WHERE
report_date=TO_DATE('Sep 2007','MON YYYY') AND group_name = 'T1';

The query works fine, except for when there is nothing to SUM and then
I get returned NULL instead of 0.

My question is how can I get the query to display 0 instead of
'blank'; I read something about using a join, but I don't see how
joining would help me as right now this is the only table in my
database. Would I somehow join against a query to DUAL?


SELECT SUM(nvl(cases_escalated, 0)) AS casesescalated
FROM groupsummary
WHERE report_date=TO_DATE('Sep 2007','MON YYYY')
AND group_name = 'T1';



David Fitzjarrell

All aggregate functions (except count(*) and grouping)
http://download.oracle.com/docs/cd/B...001.htm#i89203
ignore nulls, so there is no point to sum 0 instead of null, because the
result is the same.
To display 0 in place of NULL one could do

SELECT nvl(SUM(cases_escalated),0) AS casesescalated
FROM groupsummary
WHERE report_date=TO_DATE('Sep 2007','MON YYYY')
AND group_name = 'T1'

however, i would still prefer NULL to be distinguished from 0

Best regards

Maxim


Reply With Quote
  #5  
Old   
Ken Denny
 
Posts: n/a

Default Re: oracle sum display zero instead of NULL - 11-02-2007 , 08:38 AM



On Nov 1, 3:09 pm, Frank van Bortel <frank.van.bor... (AT) gmail (DOT) com>
wrote:
Quote:
trp... (AT) gmail (DOT) com wrote:
I have a simple query:

SELECT SUM(cases_escalated) AS casesescalated FROM groupsummary WHERE
report_date=TO_DATE('Sep 2007','MON YYYY') AND group_name = 'T1';

The query works fine, except for when there is nothing to SUM and then
I get returned NULL instead of 0.

My question is how can I get the query to display 0 instead of
'blank'; I read something about using a join, but I don't see how
joining would help me as right now this is the only table in my
database. Would I somehow join against a query to DUAL?

By not summing NULLs: sum(nvl(cases_escalated,0)),
or by decoding the NULL: nvl(sum(cases_escalated),0)

Which ever way you want it
--
Regards,
Frank van Bortel

Top-posting is one way to shut me up...
Use nvl(sum... rather than sum(nvl...
sum(nvl... will still return null if there are no rows that satisfy
the "where" clause.

SQL> select sum(nvl(null,0)) from dual where 0=1;

SUM(NVL(NULL,0))
----------------


SQL> select nvl(sum(null),0) from dual where 0=1;

NVL(SUM(NULL),0)
----------------
0



Reply With Quote
Reply




Thread Tools
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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.