Re: LEAST and GREATEST functions?

From: Ang Chin Han <angch(at)bytecraft(dot)com(dot)my>
To: Greg Stark <gsstark(at)mit(dot)edu>
Cc: Josh Berkus <josh(at)agliodbs(dot)com>, Stefan Bill <sjb26(at)yahoo(dot)com>, pgsql-sql(at)postgresql(dot)org
Subject: Re: LEAST and GREATEST functions?
Date: 2003-07-02 04:02:10
Message-ID: 3F025942.6000507@bytecraft.com.my
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Greg Stark wrote:

> MAX and MIN are single-parameter aggregate functions. LEAST and GREATEST are
> two-parameter (though in postgres they could be defined for 3 and more
> parameters) scalar functions.

If LEAST and GREATEST can accept any number of parameters, wouldn't it
make sense to code it like the way COALESCE works, rather than defining
a function for it? This way we don't need define all the various
functions with different types.

e.g.

SELECT greatest(a, b, c) FROM bar;

becomes

SELECT greatest(a, greatest(b, c)) from bar;

becomes

SELECT
CASE WHEN b < c
THEN
CASE WHEN c < a
THEN a
ELSE c
END
ELSE
CASE WHEN b < a
THEN a
ELSE b
END
END
FROM bar;

From the docs:

COALESCE and NULLIF are just shorthand for CASE expressions. They are
actually converted into CASE expressions at a very early stage of
processing, and subsequent processing thinks it is dealing with CASE.
Thus an incorrect COALESCE or NULLIF usage may draw an error message
that refers to CASE.

--
Linux homer 2.4.18-14 #1 Wed Sep 4 13:35:50 EDT 2002 i686 i686 i386
GNU/Linux
11:30am up 188 days, 2:35, 5 users, load average: 5.19, 5.08, 5.02

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Stephan Szabo 2003-07-02 04:14:49 Re: Break referential integrity.
Previous Message Rudi Starcevic 2003-07-02 03:59:50 Break referential integrity.