Search Postgresql Archives

Re: Largest & Smallest Functions

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi,

> 2) Is there any particular reason functions like that aren't built
> into Postgres?  They seem like they would be useful.  (Or maybe I
> missed them?)

LEAST() and GREATEST() expressions do the same thing as yours smallest() and largest(). See https://www.postgresql.org/docs/current/functions-conditional.html

That might also answer the first question - just drop smallest() and largest() and you will get two functions instead of four :-)

Now to be a little more serious, if you want a single function to both support variadic number of arguments AND all of them in a single array, how could the function decide whether smallest(ARRAY[1,2,3]) shall return 1 or ARRAY[1,2,3] (which is the smallest out of all arguments)? I would suggest not to declare such overloaded function even if it was possible, as it might confuse the reader easily. Instead, I would go for
SELECT min(u) FROM unnest(ARRAY[1,2,3]) u
or just define a separate least_array() / greatest_array() variant.

Regards,
Ondřej Bouda




[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [Postgresql Jobs]     [Postgresql Admin]     [Postgresql Performance]     [Linux Clusters]     [PHP Home]     [PHP on Windows]     [Kernel Newbies]     [PHP Classes]     [PHP Books]     [PHP Databases]     [Postgresql & PHP]     [Yosemite]

  Powered by Linux