pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Claus Guttesen <kometen@gmail.com>
To: Gary Stainburn <gary.stainburn@ringways.co.uk>
Cc: pgsql-sql@postgresql.org
Subject: Re: simple (?) join
Date: Sat, 26 Sep 2009 00:36:16 +0200
Message-ID: <b41c75520909251536p605e4838q8299a7a00d440982@mail.gmail.com> (raw)
In-Reply-To: <200909241616.36933.gary.stainburn@ringways.co.uk>
References: <200909241616.36933.gary.stainburn@ringways.co.uk>

> I have two tables
>
> create table orders (
> o_id serial primary key
> ...
> );
>
> create table orders_log (
> ol_id serial primary key,
> o_id int4 not null references orders(o_id),
> ol_timestamp timestamp,
> ol_user,
> );
>
> How can I select all from orders and the last (latest) entry from the
> orders_log?

Maby this will work?

Get the unique o_id from the table orders and do a group by in a
subselect, and then select the other fields from the same table, ie:

select o_od, max(o_field1), max(o_field2) ... from orders where o_id
in (select o_id from orders group by o_id) group by o_id order by
o_id;

This off course assumes that o_id is a serial (and thus that timestamp
will always be higher etc.).

-- 
regards
Claus

When lenity and cruelty play for a kingdom,
the gentler gamester is the soonest winner.

Shakespeare



view thread (25+ messages)  latest in thread

Message-ID: <b41c75520909251536p605e4838q8299a7a00d440982@mail.gmail.com>
Permalink:  ../b41c75520909251536p605e4838q8299a7a00d440982@mail.gmail.com/
Also on:    postgresql.org/message-id/b41c75520909251536p605e4838q8299a7a00d440982@mail.gmail.com

 · 

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 using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: kometen@gmail.com, gary.stainburn@ringways.co.uk
  Subject: Re: simple (?) join
  In-Reply-To: <b41c75520909251536p605e4838q8299a7a00d440982@mail.gmail.com>

* 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