X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by postgresql.org (Postfix) with ESMTP id 4F9D19DCD3F for ; Fri, 24 Mar 2006 16:49:16 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 46993-04 for ; Fri, 24 Mar 2006 16:49:17 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey- Received: from gozer.look.ca (epsilon1.look.ca [207.136.80.10]) by postgresql.org (Postfix) with ESMTP id 2DF3E9DCD22 for ; Fri, 24 Mar 2006 16:49:14 -0400 (AST) Received: from 209-161-222-80.dsl.look.ca ([209.161.222.80]) by gozer.look.ca with esmtp (Exim 4.20) id 1FMtDf-0006FU-2d; Fri, 24 Mar 2006 20:49:15 +0000 From: Rod Taylor To: Amos Hayes Cc: pgsql-sql@postgresql.org In-Reply-To: References: Date: Fri, 24 Mar 2006 15:46:55 -0500 Message-Id: <1143233215.90799.91.camel@home> Mime-Version: 1.0 X-Mailer: Evolution 2.4.2.1 FreeBSD GNOME Team Port X-SA-Exim-Mail-From: pg@rbt.ca Subject: Re: Find min and max values across two columns? Content-Type: text/plain Content-Transfer-Encoding: 7bit X-SA-Exim-Version: 3.1 (built Tue Feb 24 05:09:27 GMT 2004) X-SA-Exim-Scanned: Yes X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, score=0.093 required=5 tests=[AWL=0.093] X-Spam-Score: 0.093 X-Spam-Level: X-Archive-Number: 200603/391 X-Sequence-Number: 24737 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; --