pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Amos Hayes <ahayes@polkaroo.net>
To: pgsql-sql@postgresql.org
Subject: Find min and max values across two columns?
Date: Fri, 24 Mar 2006 15:19:38 -0500
Message-ID: <DAD4674C-1896-4F24-B43B-B884491DB931@polkaroo.net> (raw)

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



view thread (11+ messages)  latest in thread

Message-ID: <DAD4674C-1896-4F24-B43B-B884491DB931@polkaroo.net>
Permalink:  ../DAD4674C-1896-4F24-B43B-B884491DB931@polkaroo.net/
Also on:    postgresql.org/message-id/DAD4674C-1896-4F24-B43B-B884491DB931@polkaroo.net

 · 

reply

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 from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-sql@postgresql.org
  Cc: ahayes@polkaroo.net
  Subject: Re: Find min and max values across two columns?
  In-Reply-To: <DAD4674C-1896-4F24-B43B-B884491DB931@polkaroo.net>
  References: <DAD4674C-1896-4F24-B43B-B884491DB931@polkaroo.net>

* 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