dbTalk Databases Forums  

[BUGS] Should Aggregate Functions always return one row?

mailing.database.pgsql-bugs mailing.database.pgsql-bugs


Discuss [BUGS] Should Aggregate Functions always return one row? in the mailing.database.pgsql-bugs forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
wilhelm.pakulla@gmx.de
 
Posts: n/a

Default [BUGS] Should Aggregate Functions always return one row? - 01-24-2004 , 06:34 PM






Hello.

Is this a bug?

Quote:
linux=# SELECT * FROM test;
x
---
1
2
3
(3 Rows)

linux=# SELECT * FROM test WHERE FALSE;
x
---
(0 Rows)

phlegma=# SELECT max(x) FROM test WHERE FALSE;
max
-----

(1 Rows)

phlegma=#
So, if I use the max() aggregate, I receive one row with the value NULL. Is
that correct?

My background is the language plpgsql:

I expected to test for the existance of a maximum with
(...)
SELECT INTO e
max( y ) AS x FROM (table) WHERE (condition);

IF NOT FOUND THEN RETURN; END IF;
(...)

But I have to test with:

(...)
SELECT INTO e
max( y ) AS x FROM (table) WHERE (condition);

IF e.x IS NULL THEN RETURN; END IF;
(...)

Thanks in advance,
Wilhelm

---------------------------(end of broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings


Reply With Quote
  #2  
Old   
Tom Lane
 
Posts: n/a

Default Re: [BUGS] Should Aggregate Functions always return one row? - 01-24-2004 , 08:50 PM






wilhelm.pakulla (AT) gmx (DOT) de writes:
Quote:
So, if I use the max() aggregate, I receive one row with the value NULL. Is
that correct?
Yup, that's what the SQL spec says to do, and it seems reasonable to me.

IIRC, the spec also says that SUM() over no rows returns NULL, which is
less reasonable --- I'd have defined it as returning zero, myself.
But when in doubt we follow the spec.

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend


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.