dbTalk Databases Forums  

Nulls and aggregate functions

comp.databases.ingres comp.databases.ingres


Discuss Nulls and aggregate functions in the comp.databases.ingres forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Roy Hann
 
Posts: n/a

Default Nulls and aggregate functions - 11-19-2010 , 07:03 AM






I have a rather annoying little problem thanks to the way SQL treats
nulls in aggregate functions. Normally nulls are ignored in
aggregates, like they are interpretted as meaning "inapplicable".

I have a situation where null definitely means "unknown", so ignoring
the nulls is incorrect and the aggregate (sum() in this case) should
evaluate to null if there is a null input (which is what would happen
in an ordinary SQL expression).

Can anyone think of a nicer way to do it than using a case expression
like this?

select case when count(n) <> case(*) then null
else sum(n) end as total
from foo;

--
Roy

UK Ingres User Association Conference 2011 will be on Tuesday June 7 2011.
Put the date in your diary today.

Reply With Quote
  #2  
Old   
Ingres Forums
 
Posts: n/a

Default Re: Nulls and aggregate functions - 11-19-2010 , 08:10 AM






Does using if instead of case qualify as nicer? (Probably not!...)

select if(count(a)=count(ifnull(a,0)),sum(a)) from foo;


--
geraintjones

Reply With Quote
  #3  
Old   
Roy Hann
 
Posts: n/a

Default Re: Nulls and aggregate functions - 11-19-2010 , 09:56 AM



Ingres Forums wrote:

Quote:
Does using if instead of case qualify as nicer? (Probably not!...)

select if(count(a)=count(ifnull(a,0)),sum(a)) from foo;
Blech!

"Hell no," is my answer. :-)

The unstated background to my question is that my real SELECT statement
is already very gnarly and having to smother it with extra curlicues and
flourishes isn't doing anything for its readability. :-(

I hate SQL....

--
Roy

UK Ingres User Association Conference 2011 will be on Tuesday June 7 2011.
Put the date in your diary today.

Reply With Quote
  #4  
Old   
Roy Hann
 
Posts: n/a

Default Re: Nulls and aggregate functions - 11-19-2010 , 09:59 AM



Roy Hann wrote:

Quote:
select case when count(n) <> case(*) then null
else sum(n) end as total
from foo;

So far no one has remarked on the stupid type above. That *should*
have been:

select case when count(n) <> count(*) then null
else sum(n) end as total
from foo;

--
Roy

UK Ingres User Association Conference 2011 will be on Tuesday June 7 2011.
Put the date in your diary today.

Reply With Quote
  #5  
Old   
Roy Hann
 
Posts: n/a

Default Re: Nulls and aggregate functions - 11-19-2010 , 10:00 AM



Roy Hann wrote:

Quote:
So far no one has remarked on the stupid type above.
Argh! Type: a meta-typo!!

--
Roy

UK Ingres User Association Conference 2011 will be on Tuesday June 7 2011.
Put the date in your diary today.

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.