From: George Moga <george@flex.ro>
To: Julian Scarfe <jas1@scigen.co.uk>
To: SQL PostgreSQL <pgsql-sql@postgreSQL.org>
Subject: Re: [SQL] Finding the "most recent" rows
Date: Thu, 22 Apr 1999 17:46:31 +0300
Message-ID: <371F3646.C2A5C6E1@flex.ro> (raw)
References: <371F2ED6.29FD9251@scigen.co.uk>
<!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 the_place ...
<br>0910 London
<br>1130 London
<br>0910 Paris
<br>0930 London
<br>0840 Paris
<br>1020 London
<br>0740 Paris
<p>I'd like to select:
<br>1130 London
<br>0910 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> <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
|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
|time</tt>
<br><tt>----------------+-----------------------------</tt>
<br><tt>Berlin |Thu
22 Apr 17:34:31 1999 EEST</tt>
<br><tt>London |Thu
22 Apr 17:34:22 1999 EEST</tt>
<br><tt>Madrid |Thu
22 Apr 17:34:14 1999 EEST</tt>
<br><tt>Paris
|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>-- <br>
Best,<br>
George Moga,<br>
george@flex.ro<br>
Braila, ROMANIA
<br> </html>
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 from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-sql@postgresql.org
Cc: george@flex.ro, jas1@scigen.co.uk
Subject: Re: [SQL] Finding the "most recent" rows
In-Reply-To: <371F3646.C2A5C6E1@flex.ro>
References: <371F2ED6.29FD9251@scigen.co.uk> <371F3646.C2A5C6E1@flex.ro>
* 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