Re: Largest & Smallest Functions - Mailing list pgsql-general

From Ken Tanzer
Subject Re: Largest & Smallest Functions
Date
Msg-id CAD3a31W_SwE=_BzWJKZJnX2=OjKk+EDESkQrvLtu1OfFzWUU4Q@mail.gmail.com
Whole thread Raw
In response to Re: Largest & Smallest Functions  (Pavel Stehule <pavel.stehule@gmail.com>)
Responses Re: Largest & Smallest Functions
Re: Largest & Smallest Functions
List pgsql-general


On Wed, Nov 7, 2018 at 9:48 PM Pavel Stehule <pavel.stehule@gmail.com> wrote:

You can pass variadic arguments as a array

postgres=# \sf smallest
CREATE OR REPLACE FUNCTION public.smallest(VARIADIC anyarray)
 RETURNS anyelement
 LANGUAGE sql
 IMMUTABLE
AS $function$
    SELECT min($1[i]) FROM generate_subscripts($1, 1) g(i);
$function$

postgres=# select smallest(VARIADIC ARRAY[1,2,3]);
┌──────────┐
│ smallest │
╞══════════╡
│        1 │
└──────────┘
(1 row)


That's very helpful and good to know. It's too bad that doesn't work with LEAST/GREATEST, which would eliminate the need for extra functions.

Thanks!

Ken


--
AGENCY Software  
A Free Software data system
By and for non-profits
(253) 245-3801

learn more about AGENCY or
follow the discussion.

pgsql-general by date:

Previous
From: Pavel Stehule
Date:
Subject: Re: Largest & Smallest Functions
Next
From: Pavel Stehule
Date:
Subject: Re: Largest & Smallest Functions