From: Marc Sherman <msherman@projectile.ca>
To: pgsql-sql List <pgsql-sql@postgresql.org>
Subject: Select most recent record?
Date: Wed, 16 May 2001 09:28:29 -0400
Message-ID: <CGEPKMKAIFJINAOACFFEOEJDCGAA.msherman@projectile.ca> (raw)
Hi, I was hoping I could get some help with a select statement.
I have a log table with three columns: id int4, timestamp datetime,
value int4.
For any given ID, there will be a large number of rows, with
different timestamps and values.
I'd like to select the newest (max(timestamp)) row for each id,
before a given cutoff date; is this possible?
The best I've been able to come up with is the rather ugly (and
very slow):
select * from log as l1 where timestamp in
(select max(timestamp) from log where id=l1.id and
timestamp<'2001-01-01' group by id);
There must be a better way to do this; any tips?
Thanks,
- Marc
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: msherman@projectile.ca
Subject: Re: Select most recent record?
In-Reply-To: <CGEPKMKAIFJINAOACFFEOEJDCGAA.msherman@projectile.ca>
* 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