Re: LEAST and GREATEST functions? - Mailing list pgsql-sql

From Ang Chin Han
Subject Re: LEAST and GREATEST functions?
Date
Msg-id 3F025942.6000507@bytecraft.com.my
Whole thread Raw
In response to Re: LEAST and GREATEST functions?  (Greg Stark <gsstark@mit.edu>)
Responses Re: LEAST and GREATEST functions?  (Tom Lane <tgl@sss.pgh.pa.us>)
List 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

pgsql-sql by date:

Previous
From: "Jonathan Man"
Date:
Subject: Timeout for lock table
Next
From: Rudi Starcevic
Date:
Subject: Break referential integrity.