From: Tom Lane <tgl@sss.pgh.pa.us>
To: pgsql-sql@postgresql.org <pgsql-sql@postgreSQL.org>
Subject: Re: [SQL] Finding the "most recent" rows
Date: Thu, 22 Apr 1999 11:29:31 -0400
Message-ID: <17966.924794971@sss.pgh.pa.us> (raw)
In-Reply-To: <371F35F9.607AF4B7@bigfoot.com>
Chris Bitmead <chris.bitmead@bigfoot.com> writes:
> Julian Scarfe wrote:
>> I'd like an efficient way to pull out the most recent row (i.e. highest
>> datatime) belonging to *each* of a number of places selected by a simple
>> query.
>
> Try
> SELECT the_place, max(the_time) FROM the_place GROUP BY the_place;
But I'll bet he wants the whole row containing the max time, not just
the place and time columns. I've run into similar problems and never
felt like I had a clean solution, either.
You could do something like
SELECT * FROM table AS t1 WHERE NOT
EXISTS(SELECT * FROM table AS t2 WHERE
t2.place = t1.place AND t2.time > t1.time);
but this is ugly, and probably horribly inefficient as well. (It might
not be unacceptably slow if the table has indexes on place and time,
but it sure looks like a brute-force approach.)
What you'd really like is something like a SELECT DISTINCT with a user-
specifiable row comparison operator; then you'd just "ORDER BY place, time"
and make a comparator that discards all but the last row for each place
value. Hmm ... a little experimentation suggests that
SELECT DISTINCT ON place * FROM table ORDER BY place, time DESC;
might do the right thing. It *seems* to select the first row for each
value of place. I've never seen a spec for this feature, however, so
I'm not sure if it's reliable or not...
regards, tom lane
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: tgl@sss.pgh.pa.us
Subject: Re: [SQL] Finding the "most recent" rows
In-Reply-To: <17966.924794971@sss.pgh.pa.us>
References: <371F35F9.607AF4B7@bigfoot.com> <17966.924794971@sss.pgh.pa.us>
* 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