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

From: George Moga <george(at)flex(dot)ro>
To: Julian Scarfe <jas1(at)scigen(dot)co(dot)uk>, SQL PostgreSQL <pgsql-sql(at)postgreSQL(dot)org>
Subject: Re: [SQL] Finding the "most recent" rows
Date: 1999-04-22 14:46:31
Message-ID: 371F3646.C2A5C6E1@flex.ro
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
Julian Scarfe wrote:
<blockquote TYPE=CITE>I have a table (representing a set of observations)
with datetime fields and a
<br>non-unique place field.
<p>e.g.
<br>create table obs (
<br>the_time datetime,
<br>the_place char(8),
<br>...other fields...
<br>)
<p>I'd like an efficient way to pull out the most recent row (i.e. highest
<br>datatime) belonging to *each* of a number of places selected by a simple
<br>query.
<p>e.g. given a table such as:
<p>the_time&nbsp;&nbsp;&nbsp; the_place&nbsp;&nbsp; ...
<br>0910&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; London
<br>1130&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; London
<br>0910&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Paris
<br>0930&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; London
<br>0840&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Paris
<br>1020&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; London
<br>0740&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Paris
<p>I'd like to select:
<br>1130&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; London
<br>0910&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Paris
<p>Most of my attempts at this (as an SQL novice) feel very clumsy and
<br>inefficient. Is there an efficient way of doing this in SQL?
<br>--
<p>Julian Scarfe</blockquote>
If I understund the problem try this:
<br>&nbsp;<tt></tt>
<p><tt>test=> create table test (time datetime default now(), place char(16));</tt>
<br><tt>CREATE</tt>
<br><tt>test=> insert into test (place) values ('London');</tt>
<br><tt>INSERT 194824 1</tt>
<br><tt>test=> insert into test (place) values ('Paris');</tt>
<br><tt>INSERT 194825 1</tt>
<br><tt>.............</tt>
<br><tt>test=> insert into test (place) values ('Berlin');</tt>
<br><tt>INSERT 194835 1</tt>
<br><tt>test=> insert into test (place) values ('London');</tt>
<br><tt>INSERT 194836 1</tt>
<br><tt>test=> insert into test (place) values ('Berlin');</tt>
<br><tt>INSERT 194837 1</tt>
<br><tt>test=> select * from test;</tt>
<br><tt>time&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
|place</tt>
<br><tt>-----------------------------+----------------</tt>
<br><tt>Thu 22 Apr 17:33:23 1999 EEST|London</tt>
<br><tt>Thu 22 Apr 17:33:30 1999 EEST|Paris</tt>
<br><tt>Thu 22 Apr 17:33:36 1999 EEST|London</tt>
<br><tt>Thu 22 Apr 17:33:49 1999 EEST|Madrid</tt>
<br><tt>Thu 22 Apr 17:33:54 1999 EEST|Paris</tt>
<br><tt>Thu 22 Apr 17:34:03 1999 EEST|Berlin</tt>
<br><tt>Thu 22 Apr 17:34:05 1999 EEST|Madrid</tt>
<br><tt>Thu 22 Apr 17:34:08 1999 EEST|London</tt>
<br><tt>Thu 22 Apr 17:34:12 1999 EEST|Paris</tt>
<br><tt>Thu 22 Apr 17:34:14 1999 EEST|Madrid</tt>
<br><tt>Thu 22 Apr 17:34:16 1999 EEST|Paris</tt>
<br><tt>Thu 22 Apr 17:34:20 1999 EEST|Berlin</tt>
<br><tt>Thu 22 Apr 17:34:22 1999 EEST|London</tt>
<br><tt>Thu 22 Apr 17:34:31 1999 EEST|Berlin</tt>
<br><tt>(14 rows)</tt><tt></tt>
<p><tt>test=> select place, time from test t where time = (select max(s.time)
from test s where s.place = t.place) order by place;</tt>
<br><tt>place&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
|time</tt>
<br><tt>----------------+-----------------------------</tt>
<br><tt>Berlin&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |Thu
22 Apr 17:34:31 1999 EEST</tt>
<br><tt>London&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |Thu
22 Apr 17:34:22 1999 EEST</tt>
<br><tt>Madrid&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |Thu
22 Apr 17:34:14 1999 EEST</tt>
<br><tt>Paris&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
|Thu 22 Apr 17:34:16 1999 EEST</tt>
<br><tt>(4 rows)</tt><tt></tt>
<p><tt>test=></tt>
<p>I use PostgreSQL 6.5.0 beta1 on Red Hat LINUX 5.2 with 2.2.2 kernel.
<p>--&nbsp;<br>
Best,<br>
&nbsp; George Moga,<br>
&nbsp; george(at)flex(dot)ro<br>
&nbsp; Braila, ROMANIA
<br>&nbsp;</html>

Attachment Content-Type Size
unknown_filename text/html 3.8 KB

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Michael J Davis 1999-04-22 14:53:18 RE: [SQL] Finding the "most recent" rows
Previous Message Chris Bitmead 1999-04-22 14:45:13 Re: [SQL] Finding the "most recent" rows