From: Tom Lane <tgl@sss.pgh.pa.us>
To: Andrew Perrin <andrew_perrin@unc.edu>
Cc: Marc Sherman <msherman@projectile.ca>
Cc: pgsql-sql List <pgsql-sql@postgresql.org>
Subject: Re: Select most recent record?
Date: Wed, 16 May 2001 10:50:17 -0400
Message-ID: <17280.990024617@sss.pgh.pa.us> (raw)
In-Reply-To: <3B02920A.B6340FC1@unc.edu>
References: <CGEPKMKAIFJINAOACFFEOEJDCGAA.msherman@projectile.ca>
<17169.990023757@sss.pgh.pa.us>
<3B02920A.B6340FC1@unc.edu>
Andrew Perrin <andrew_perrin@unc.edu> writes:
> Except that he wants max(timestamp) by id; perhaps a GROUP BY would
> help, something like (untested):
Oh, I'm sorry, I missed the "for each id" bit.
> select max(timestamp) from log group by id;
That gives you the max timestamp for each id, all right, but not the
rest of the row in which the max timestamp occurs. I know of no good
way to get that in standard SQL. If you don't mind being nonstandard,
this is exactly the problem SELECT DISTINCT ON is meant for:
select distinct on (id) * from log order by id, timestamp desc;
See the SELECT reference page for more about this.
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 using the --to and --cc options:
reply via email
To: pgsql-sql@postgresql.org
Cc: tgl@sss.pgh.pa.us, andrew_perrin@unc.edu, msherman@projectile.ca
Subject: Re: Select most recent record?
In-Reply-To: <17280.990024617@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