pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Louis-David Mitterrand <cunctator@apartia.ch>
To: Gerhard Dieringer <DieringG@eba-haus.de>
Cc: " <" <pgsql-sql@postgresql.org>
Subject: Re: Antw: using max() aggregate
Date: Fri, 16 Jun 2000 12:54:53 +0200
Message-ID: <20000616125453.A501@styx> (raw)
In-Reply-To: <s94a07de.061@kopo001>
References: <s94a07de.061@kopo001>

On Fri, Jun 16, 2000 at 10:56:04AM +0200, Gerhard Dieringer wrote:
> > Yes this would work nicely but if I need to add more conditional clauses
> > I have to duplicate them in the main SELECT and in the sub-SELECT:
> >
> > SELECT title,stopdate
> > FROM auction
> > WHERE stopdate = (SELECT max(stopdate) FROM auction WHERE stopdate > now()) 
> > AND stopdate > now();
> >
> > Or am I missing something?
> >
> > Tom Lane suggested using:
> >
> > SELECT title,stopdate FROM auction ORDER BY stopdate LIMIT 1;
> >
> > which seems the best solution (I was a bit concerned about performance,
> > but then again the max() aggregate does a scan of all rows as well).
> > ...
> 
> I don't see why you repeat your conditions in the outer select. The
> condition in the inner select drops all records that violate the
> conditions, so the same conditions in the outer select have nothing to
> do and you can leave them away.

Maybe mine was a bad example but if, for instance, you add a condition
on the "login" attribute (that it should start with a 'm'), then if you
omit the clause from the outer select you risk having a false match if
two records have the same stopdate:

SELECT title,login,stopdate
FROM auction
WHERE stopdate = (SELECT max(stopdate) FROM auction WHERE login LIKE 'm%');

> Tom's solution has the drawback, that if you have more than one record
> with the same max value you only get one of them, but may be that you
> want to see all of them.

True.

Thanks,

-- 
Louis-David Mitterrand - ldm@apartia.org - http://www.apartia.fr

 Save the whales. Feed the hungry. Free the mallocs. 



view thread (4+ messages)

Message-ID: <20000616125453.A501@styx>
Permalink:  ../20000616125453.A501@styx/
Also on:    postgresql.org/message-id/20000616125453.A501@styx

 · 

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: cunctator@apartia.ch, DieringG@eba-haus.de, " <pgsql-sql@postgresql.org
  Subject: Re: using max() aggregate
  In-Reply-To: <20000616125453.A501@styx>
  References: <s94a07de.061@kopo001> <20000616125453.A501@styx>

* 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