Thread: new max function

new max function

From
Rodrigo Gesswein
Date:
Hello!
  I'm looking for a function to calculate the max value from two numbers,
something like max2(a,b) with a,b int
  Does anyone have the trick ?
  Thank you in advance..

Rodrigo!


Re: new max function

From
Mike Rylander
Date:
Here's mine:

CREATE FUNCTION max2 (INTEGER,INTEGER) RETURNS INTEGER LANGUAGE SQL AS 'SELECT CASE WHEN $1 > $2 THEN $1 ELSE $2 END';

This returns:

database=# select max2(1,2);max2 
------   2
(1 row)

database=# select max2(3,1);max2 
------   3
(1 row)


On Friday 17 October 2003 02:13 pm, Rodrigo Gesswein wrote:
> Hello!
>
>    I'm looking for a function to calculate the max value from two numbers,
> something like max2(a,b) with a,b int
>
>    Does anyone have the trick ?
>
>    Thank you in advance..
>
> Rodrigo!
>
> ---------------------------(end of broadcast)---------------------------
> TIP 3: if posting/reading through Usenet, please send an appropriate
>       subscribe-nomail command to majordomo@postgresql.org so that your
>       message can get through to the mailing list cleanly

-- 
Mike Rylander



Re: new max function

From
Tom Lane
Date:
Mike Rylander <miker@n2bb.com> writes:
> Here's mine:

> CREATE FUNCTION max2 (INTEGER,INTEGER) RETURNS INTEGER
>   LANGUAGE SQL AS
>   'SELECT CASE WHEN $1 > $2 THEN $1 ELSE $2 END';

BTW, most of the standard datatypes have these already, because they are
the transition functions for the MAX() and MIN() aggregates.  The one
above is int4larger().

regression=# \df *larger                                                   List of functions     Result data type
|  Schema   |        Name        |                   Argument data types
 

-----------------------------+------------+--------------------+----------------------------------------------------------money
                     | pg_catalog | cashlarger         | money, moneydate                        | pg_catalog |
date_larger       | date, datereal                        | pg_catalog | float4larger       | real, realdouble
precision           | pg_catalog | float8larger       | double precision, double precisionsmallint                    |
pg_catalog| int2larger         | smallint, smallintinteger                     | pg_catalog | int4larger         |
integer,integerbigint                      | pg_catalog | int8larger         | bigint, bigintinterval
| pg_catalog | interval_larger    | interval, intervalnumeric                     | pg_catalog | numeric_larger     |
numeric,numericoid                         | pg_catalog | oidlarger          | oid, oidtext                        |
pg_catalog| text_larger        | text, texttime without time zone      | pg_catalog | time_larger        | time without
timezone, time without time zonetimestamp without time zone | pg_catalog | timestamp_larger   | timestamp without time
zone,timestamp without time zonetimestamp with time zone    | pg_catalog | timestamptz_larger | timestamp with time
zone,timestamp with time zonetime with time zone         | pg_catalog | timetz_larger      | time with time zone, time
withtime zone
 
(15 rows)

All of these have matching xxxsmaller() as well.
        regards, tom lane