minimum function

Started by Gunther Mayeralmost 19 years ago6 messagesgeneral
Jump to latest
#1Gunther Mayer
gunther.mayer@googlemail.com

Hi there,

I'm busy writing a trigger function in pl/pgsql and find myself in need
of a minimum() function. I can't see how the builtin min() aggregate
function can be of any use here since all I want to do is something like

SELECT minimum(5,6) => 5

Any way I can achieve that on one line? I.e. I want it simpler than

IF arg1 < arg2 THEN
RETURN arg1;
ELSE
RETURN arg2;
END IF;

Gunther

In reply to: Gunther Mayer (#1)
Re: minimum function

On 23/06/2007 17:17, Gunther Mayer wrote:

Any way I can achieve that on one line? I.e. I want it simpler than

IF arg1 < arg2 THEN
RETURN arg1;
ELSE
RETURN arg2;
END IF;

That looks pretty simple already, but why not enclose it in a pl/pgsql
function - something like:

create function minimum(a1 integer, a2 integer) returns integer as
$$
begin
if a1 < a2 then
return a1;
else
return a2;
end if;
end;
$$
language plpgsql;

- and then you can call it in one line:

select minimum(5, 4);

Ray.

---------------------------------------------------------------
Raymond O'Donnell, Director of Music, Galway Cathedral, Ireland
rod@iol.ie
---------------------------------------------------------------

#3PFC
lists@peufeu.com
In reply to: Raymond O'Donnell (#2)
Re: minimum function

Check out greatest() and least()... (I think ;)

On Sat, 23 Jun 2007 18:35:36 +0200, Raymond O'Donnell <rod@iol.ie> wrote:

Show quoted text

On 23/06/2007 17:17, Gunther Mayer wrote:

Any way I can achieve that on one line? I.e. I want it simpler than
IF arg1 < arg2 THEN
RETURN arg1;
ELSE
RETURN arg2;
END IF;

That looks pretty simple already, but why not enclose it in a pl/pgsql
function - something like:

create function minimum(a1 integer, a2 integer) returns integer as
$$
begin
if a1 < a2 then
return a1;
else
return a2;
end if;
end;
$$
language plpgsql;

- and then you can call it in one line:

select minimum(5, 4);

Ray.

---------------------------------------------------------------
Raymond O'Donnell, Director of Music, Galway Cathedral, Ireland
rod@iol.ie
---------------------------------------------------------------

---------------------------(end of broadcast)---------------------------
TIP 4: Have you searched our list archives?

http://archives.postgresql.org/

#4Martijn van Oosterhout
kleptog@svana.org
In reply to: Gunther Mayer (#1)
Re: minimum function

On Sat, Jun 23, 2007 at 06:17:03PM +0200, Gunther Mayer wrote:

Hi there,

I'm busy writing a trigger function in pl/pgsql and find myself in need
of a minimum() function. I can't see how the builtin min() aggregate
function can be of any use here since all I want to do is something like

SELECT minimum(5,6) => 5

There are the functions int4larger/int4smaller. There are equivalent
function for other types.

Have a nice day,
--
Martijn van Oosterhout <kleptog@svana.org> http://svana.org/kleptog/

Show quoted text

From each according to his ability. To each according to his ability to litigate.

#5Tom Lane
tgl@sss.pgh.pa.us
In reply to: Gunther Mayer (#1)
Re: minimum function

Gunther Mayer <gunther.mayer@googlemail.com> writes:

SELECT minimum(5,6) => 5

You're looking for the least/greatest functions (in 8.1 and up IIRC).

regards, tom lane

#6Gunther Mayer
gunther.mayer@googlemail.com
In reply to: Tom Lane (#5)
Re: minimum function

Tom Lane wrote:

Gunther Mayer <gunther.mayer@googlemail.com> writes:

SELECT minimum(5,6) => 5

You're looking for the least/greatest functions (in 8.1 and up IIRC).

regards, tom lane

Awesome, that's exactly what I was looking for. My pl/pgsql minimum()
hack is gonna go in the dumpster now ;-)

Thanks so much guys.

Gunther

P.S.: Can't believe I didn't spot that in the documentation, that's what
happens in the rare cases of using the wrong search terms...