Thread: minimum function

minimum function

From
Gunther Mayer
Date:
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

Re: minimum function

From
Raymond O'Donnell
Date:
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
---------------------------------------------------------------

Re: minimum function

From
PFC
Date:

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


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

> 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/



Re: minimum function

From
Martijn van Oosterhout
Date:
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/
> From each according to his ability. To each according to his ability to litigate.

Attachment

Re: minimum function

From
Tom Lane
Date:
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

Re: minimum function

From
Gunther Mayer
Date:
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...