Re: [SQL] Finding the "most recent" rows

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: "pgsql-sql(at)postgresql(dot)org" <pgsql-sql(at)postgreSQL(dot)org>
Subject: Re: [SQL] Finding the "most recent" rows
Date: 1999-04-22 15:29:31
Message-ID: 17966.924794971@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Chris Bitmead <chris(dot)bitmead(at)bigfoot(dot)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

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Kyle Bateman 1999-04-22 17:26:23 sum of two queries
Previous Message Michael J Davis 1999-04-22 14:53:18 RE: [SQL] Finding the "most recent" rows