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 7B0729DCC1F for ; Fri, 24 Mar 2006 16:19:44 -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 39114-10 for ; Fri, 24 Mar 2006 16:19:45 -0400 (AST) Received: from svr4.postgresql.org (svr4.postgresql.org [66.98.251.159]) by postgresql.org (Postfix) with ESMTP id 7F7ED9DCDB7 for ; Fri, 24 Mar 2006 16:19:42 -0400 (AST) Received: from keop.polkaroo.net (cpu2218.adsl.bellglobal.com [207.236.3.195]) by svr4.postgresql.org (Postfix) with ESMTP id 439555AF0D3 for ; Fri, 24 Mar 2006 20:19:43 +0000 (GMT) Received: from [134.117.194.200] (dhcp-200.gcrc.carleton.ca [134.117.194.200]) by keop.polkaroo.net (Postfix) with ESMTP id 946821BB469A for ; Fri, 24 Mar 2006 15:19:38 -0500 (EST) Mime-Version: 1.0 (Apple Message framework v746.3) Content-Transfer-Encoding: 7bit Message-Id: Content-Type: text/plain; charset=US-ASCII; delsp=yes; format=flowed To: pgsql-sql@postgresql.org From: Amos Hayes Subject: Find min and max values across two columns? Date: Fri, 24 Mar 2006 15:19:38 -0500 X-Mailer: Apple Mail (2.746.3) X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, score=1.236 required=5 tests=[RCVD_IN_SORBS_WEB=1.236] X-Spam-Score: 1.236 X-Spam-Level: * X-Archive-Number: 200603/388 X-Sequence-Number: 24734 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