On Mon, 28 Sep 2009 12:02:20 +0100, Gary Stainburn wrote about Re:
[SQL] simple (?) join:

[snip]
>For some reason the reply I sent on Friday didn't get through.
>What I need is all of the order record and all of the latest log entry 
>returning as a join. Specifically I want for each order the most
>recent log entry timestamp and it's associated user - i.e. who made
>the the last log entry and when.

In that case, try this:

SELECT o.*, ol.ol_timestamp, ol.ol_user
FROM orders AS o
INNER JOIN orders_log AS ol
ON ol.o_id = o.o_id
WHERE ol.ol_timestamp = (SELECT MAX(ol_timestamp) FROM orders_log AS
   ol2 WHERE ol2.o_id = o.o_id);

This will omit orders that have never been logged. You have not
specified what you want done with them.
-- 
Regards,

Dave  [RLU #314465]
=======================================================================
david.w.n...@ntlworld.com (David W Noon)
=======================================================================

-- 
Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-sql

Reply via email to