Skip site navigation (1) Skip section navigation (2)

Re: Antw: using max() aggregate

From: "Gerhard Dieringer" <DieringG(at)eba-haus(dot)de>
To: <cunctator(at)apartia(dot)ch>
Cc: "<"<pgsql-sql(at)postgresql(dot)org>
Subject: Re: Antw: using max() aggregate
Date: 2000-06-16 08:56:04
Message-ID: s94a07de.061@kopo001 (view raw, whole thread or download thread mbox)
Lists: pgsql-sql
Louis-David Mitterrand - ldm(at)apartia(dot)org - 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,max(stopdate)
> FROM auction
> WHERE stopdate = (SELECT max(stopdate) FROM auction AND 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.

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.



pgsql-sql by date

Next:From: Louis-David MitterrandDate: 2000-06-16 10:54:53
Subject: Re: Antw: using max() aggregate
Previous:From: Louis-David MitterrandDate: 2000-06-16 08:11:21
Subject: Re: Antw: using max() aggregate

Privacy Policy | About PostgreSQL
Copyright © 1996-2017 The PostgreSQL Global Development Group