Re: simple (?) join - Mailing list pgsql-sql

From Claus Guttesen
Subject Re: simple (?) join
Date
Msg-id b41c75520909251536p605e4838q8299a7a00d440982@mail.gmail.com
Whole thread Raw
In response to simple (?) join  (Gary Stainburn <gary.stainburn@ringways.co.uk>)
List pgsql-sql
> 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


pgsql-sql by date:

Previous
From: "Oliveiros C,"
Date:
Subject: Re: simple (?) join
Next
From: justin
Date:
Subject: Re: simple (?) join