From: Jim C. Nasby <jnasby@pervasive.com>
To: Tom Lane <tgl@sss.pgh.pa.us>
Cc: Amos Hayes <ahayes@polkaroo.net>
Cc: pgsql-sql@postgresql.org
Subject: Re: Find min and max values across two columns?
Date: Mon, 27 Mar 2006 07:56:29 -0600
Message-ID: <20060327135629.GM80726@pervasive.com> (raw)
In-Reply-To: <9558.1143234035@sss.pgh.pa.us>
References: <DAD4674C-1896-4F24-B43B-B884491DB931@polkaroo.net>
<9558.1143234035@sss.pgh.pa.us>
On Fri, Mar 24, 2006 at 04:00:35PM -0500, Tom Lane wrote:
> Amos Hayes <ahayes@polkaroo.net> writes:
> > I'm trying to build a query that among other things, returns the
> > minimum and maximum values contained in either of two columns.
>
> I think you might be looking for
>
> select greatest(max(columnA), max(columnB)) from tab;
> select least(min(columnA), min(columnB)) from tab;
>
> greatest/least are relatively new but you can roll your own in
> older PG releases.
And if you care about performance you might also try:
SELECT max(greatest(column_a, column_b) ...
SELECT min(least(column_a, column_b) ...
There may be a difference in performance between the two.
--
Jim C. Nasby, Sr. Engineering Consultant jnasby@pervasive.com
Pervasive Software http://pervasive.com work: 512-231-6117
vcard: http://jim.nasby.net/pervasive.vcf cell: 512-569-9461
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: pgsql-sql@postgresql.org
Cc: jnasby@pervasive.com, tgl@sss.pgh.pa.us, ahayes@polkaroo.net
Subject: Re: Find min and max values across two columns?
In-Reply-To: <20060327135629.GM80726@pervasive.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox