pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
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&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@flex.ro<br>
&nbsp; Braila, ROMANIA
<br>&nbsp;</html>




view thread (24+ messages)  latest in thread

Message-ID: <371F3646.C2A5C6E1@flex.ro>
Permalink:  ../371F3646.C2A5C6E1@flex.ro/
Also on:    postgresql.org/message-id/371F3646.C2A5C6E1@flex.ro

 · 

reply

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