Select most recent record? - Mailing list pgsql-sql

From Marc Sherman
Subject Select most recent record?
Date
Msg-id CGEPKMKAIFJINAOACFFEOEJDCGAA.msherman@projectile.ca
Whole thread Raw
Responses Re: Select most recent record?  (Tom Lane <tgl@sss.pgh.pa.us>)
RE: Select most recent record?  ("Mark Hamby" <mark@archer.leavenworth.army.mil>)
List pgsql-sql
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 andtimestamp<'2001-01-01'
groupby id);
 

There must be a better way to do this; any tips?

Thanks,
- Marc



pgsql-sql by date:

Previous
From: Chris Ryan
Date:
Subject: [Fwd: [Gborg-bugs] BUG: reference error when using inherited tables (ID: 269) (new)]
Next
From: Srikanth Rao
Date:
Subject: Restricting the number of decimal digits