Re: Find min and max values across two columns?

Lists: pgsql-sql
From: Amos Hayes <ahayes(at)polkaroo(dot)net>
To: pgsql-sql(at)postgresql(dot)org
Subject: Find min and max values across two columns?
Date: 2006-03-24 20:19:38
Message-ID: DAD4674C-1896-4F24-B43B-B884491DB931@polkaroo.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-sql

Hello. I've recently begun to use PostgreSQL in earnest (working with
data as opposed to just having clever applications tuck it away in
there) and have hit a wall with something.

I'm trying to build a query that among other things, returns the
minimum and maximum values contained in either of two columns. The
problem is that min() and max() only take one column name as an
argument. Is there some clever way that I can craft an expression to
find the min/max across both columns? I have yet to delve into any
pgsql so if it requires that, then please go easy on me. :)

Example:

id | columnA | columnB
1 | 4 | 2
2 | 3 | 4
2 | 5 | 1

I'd like to be able to discover that 1 is the smallest value in
either columnA or columnB and 5 is the largest. I don't actually care
what rows they are in, I just want the values.

Thanks for your time! Any help or pointers to relevant reading
material on this would be greatly appreciated. (I have been using the
excellent PostgreSQL docs and an equally good book titled Beginning
Databases with PostgreSQL by Neil Matthew and Richard Stones so far.)

--
Amos


From: Rod Taylor <pg(at)rbt(dot)ca>
To: Amos Hayes <ahayes(at)polkaroo(dot)net>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: Find min and max values across two columns?
Date: 2006-03-24 20:46:55
Message-ID: 1143233215.90799.91.camel@home
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-sql

On Fri, 2006-03-24 at 15:19 -0500, Amos Hayes wrote:
> Hello. I've recently begun to use PostgreSQL in earnest (working with
> data as opposed to just having clever applications tuck it away in
> there) and have hit a wall with something.
>
> I'm trying to build a query that among other things, returns the
> minimum and maximum values contained in either of two columns. The
> problem is that min() and max() only take one column name as an
> argument. Is there some clever way that I can craft an expression to
> find the min/max across both columns? I have yet to delve into any
> pgsql so if it requires that, then please go easy on me. :)
>
> Example:
>
> id | columnA | columnB
> 1 | 4 | 2
> 2 | 3 | 4
> 2 | 5 | 1
>
>
> I'd like to be able to discover that 1 is the smallest value in
> either columnA or columnB and 5 is the largest. I don't actually care
> what rows they are in, I just want the values.

rk=# create table tab (id integer, a integer, b integer);
CREATE TABLE
rk=# insert into tab values (1, 4, 2);
INSERT 0 1
rk=# insert into tab values (2,3,4);
INSERT 0 1
rk=# insert into tab values (2,5,1);
INSERT 0 1
rk=# select case when maxa > maxb then maxa else maxb end as max
, case when mina < minb then mina else minb end as min
from (select max(a) as maxa
, max(b) as maxb
, min(a) as mina
, min(b) as minb
from tab
) as tabalias;
max | min
-----+-----
5 | 1
(1 row)

The reason for the subselect is to prevent multiple calculations of
individual column aggregates. I believe it *may* be calculated multiple
times otherwise this would work just as well:

select case when max(a) > max(b) then max(a) else max(b) end as max from
tab;

--


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Amos Hayes <ahayes(at)polkaroo(dot)net>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: Find min and max values across two columns?
Date: 2006-03-24 21:00:35
Message-ID: 9558.1143234035@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-sql

Amos Hayes <ahayes(at)polkaroo(dot)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.

regards, tom lane


From: Rod Taylor <pg(at)rbt(dot)ca>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Amos Hayes <ahayes(at)polkaroo(dot)net>, pgsql-sql(at)postgresql(dot)org
Subject: Re: Find min and max values across two columns?
Date: 2006-03-24 21:20:06
Message-ID: 1143235206.90799.108.camel@home
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-sql

On Fri, 2006-03-24 at 16:20 -0500, Tom Lane wrote:
> Rod Taylor <pg(at)rbt(dot)ca> writes:
> > The reason for the subselect is to prevent multiple calculations of
> > individual column aggregates. I believe it *may* be calculated multiple
> > times otherwise this would work just as well:
>
> > select case when max(a) > max(b) then max(a) else max(b) end as max from
> > tab;
>
> Just for the record, we've gotten that right since 7.4. greatest()
> would be a notationally cleaner solution than CASE, but multiple
> occurrences of identical aggregates don't cost much of anything.

Thanks. I could not remember one way or the other.

--


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Rod Taylor <pg(at)rbt(dot)ca>
Cc: Amos Hayes <ahayes(at)polkaroo(dot)net>, pgsql-sql(at)postgresql(dot)org
Subject: Re: Find min and max values across two columns?
Date: 2006-03-24 21:20:31
Message-ID: 10495.1143235231@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-sql

Rod Taylor <pg(at)rbt(dot)ca> writes:
> The reason for the subselect is to prevent multiple calculations of
> individual column aggregates. I believe it *may* be calculated multiple
> times otherwise this would work just as well:

> select case when max(a) > max(b) then max(a) else max(b) end as max from
> tab;

Just for the record, we've gotten that right since 7.4. greatest()
would be a notationally cleaner solution than CASE, but multiple
occurrences of identical aggregates don't cost much of anything.

regards, tom lane


From: Amos Hayes <ahayes(at)polkaroo(dot)net>
To: Rod Taylor <pg(at)rbt(dot)ca>, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: Find min and max values across two columns?
Date: 2006-03-24 21:34:43
Message-ID: 0C305AAF-7DA6-40BF-B118-2E63C2A27748@polkaroo.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-sql


Your tips were great and I have hunted down the relevant pages in the
docs. Thanks guys!

--
Amos

On 24-Mar-06, at 4:20 PM, Rod Taylor wrote:

> On Fri, 2006-03-24 at 16:20 -0500, Tom Lane wrote:
>> Rod Taylor <pg(at)rbt(dot)ca> writes:
>>> The reason for the subselect is to prevent multiple calculations of
>>> individual column aggregates. I believe it *may* be calculated
>>> multiple
>>> times otherwise this would work just as well:
>>
>>> select case when max(a) > max(b) then max(a) else max(b) end as
>>> max from
>>> tab;
>>
>> Just for the record, we've gotten that right since 7.4. greatest()
>> would be a notationally cleaner solution than CASE, but multiple
>> occurrences of identical aggregates don't cost much of anything.
>
> Thanks. I could not remember one way or the other.
>
> --
>
>
> ---------------------------(end of
> broadcast)---------------------------
> TIP 1: if posting/reading through Usenet, please send an appropriate
> subscribe-nomail command to majordomo(at)postgresql(dot)org so that
> your
> message can get through to the mailing list cleanly


From: "Jim C(dot) Nasby" <jnasby(at)pervasive(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Amos Hayes <ahayes(at)polkaroo(dot)net>, pgsql-sql(at)postgresql(dot)org
Subject: Re: Find min and max values across two columns?
Date: 2006-03-27 13:56:29
Message-ID: 20060327135629.GM80726@pervasive.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-sql

On Fri, Mar 24, 2006 at 04:00:35PM -0500, Tom Lane wrote:
> Amos Hayes <ahayes(at)polkaroo(dot)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(at)pervasive(dot)com
Pervasive Software http://pervasive.com work: 512-231-6117
vcard: http://jim.nasby.net/pervasive.vcf cell: 512-569-9461


From: Emi Lu <emilu(at)encs(dot)concordia(dot)ca>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Amos Hayes <ahayes(at)polkaroo(dot)net>, pgsql-sql(at)postgresql(dot)org
Subject: Re: Find min and max values across two columns?
Date: 2006-05-15 21:40:40
Message-ID: 4468F558.7070407@encs.concordia.ca
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-sql

Hello,

I tried "select greatest(max(a), max(b)) from public.test", but I got
the following errors:

ERROR: function greatest(integer, integer) does not exist
HINT: No function matches the given name and argument types. You may
need to add explicit type casts.

May I know where I can read the docs about greatest & least please.

I am using PostgreSQL 8.0.7.

Thanks,
Ying

>Amos Hayes <ahayes(at)polkaroo(dot)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.
>
> regards, tom lane
>
>---------------------------(end of broadcast)---------------------------
>TIP 9: In versions below 8.0, the planner will ignore your desire to
> choose an index scan if your joining column's datatypes do not
> match
>
>


From: Scott Marlowe <smarlowe(at)g2switchworks(dot)com>
To: emilu(at)encs(dot)concordia(dot)ca
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, Amos Hayes <ahayes(at)polkaroo(dot)net>, pgsql-sql(at)postgresql(dot)org
Subject: Re: Find min and max values across two columns?
Date: 2006-05-15 21:50:08
Message-ID: 1147729808.9755.157.camel@state.g2switchworks.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-sql

On Mon, 2006-05-15 at 16:40, Emi Lu wrote:
> Hello,
>
> I tried "select greatest(max(a), max(b)) from public.test", but I got
> the following errors:
>
> ERROR: function greatest(integer, integer) does not exist
> HINT: No function matches the given name and argument types. You may
> need to add explicit type casts.
>
> May I know where I can read the docs about greatest & least please.
>
> I am using PostgreSQL 8.0.7.

You need to define your problem better. Assuming these data:

a b
12 24
24 12
13 18
25 10

which should I get for greatest(max(a), max(b)) should I get? Do you
just want 25,24??? In that case you don't need greatest. Do you need
the highest total between the two, or what?


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: emilu(at)encs(dot)concordia(dot)ca
Cc: Amos Hayes <ahayes(at)polkaroo(dot)net>, pgsql-sql(at)postgresql(dot)org
Subject: Re: Find min and max values across two columns?
Date: 2006-05-16 00:08:05
Message-ID: 441.1147738085@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-sql

Emi Lu <emilu(at)encs(dot)concordia(dot)ca> writes:
> ERROR: function greatest(integer, integer) does not exist
> HINT: No function matches the given name and argument types. You may
> need to add explicit type casts.

We added greatest/least in 8.1, but before that you can just use a
CASE expression instead, along the lines of
case when x>y then x else y end

regards, tom lane


From: Emi Lu <emilu(at)encs(dot)concordia(dot)ca>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: Find min and max values across two columns?
Date: 2006-05-16 14:44:07
Message-ID: 4469E537.6010108@encs.concordia.ca
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-sql

Thank you Tom.

>Emi Lu <emilu(at)encs(dot)concordia(dot)ca> writes:
>
>
>>ERROR: function greatest(integer, integer) does not exist
>>HINT: No function matches the given name and argument types. You may
>>need to add explicit type casts.
>>
>>
>
>We added greatest/least in 8.1, but before that you can just use a
>CASE expression instead, along the lines of
> case when x>y then x else y end
>
> regards, tom lane
>
>---------------------------(end of broadcast)---------------------------
>TIP 6: explain analyze is your friend
>
>