Re: simple (?) join

From: David W Noon <dwnoon(at)ntlworld(dot)com>
To: pgsql-sql(at)postgresql(dot)org
Subject: Re: simple (?) join
Date: 2009-09-28 11:27:16
Message-ID: 20090928122716.61b99cfb@dwnoon.ntlworld.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-sql

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(dot)w(dot)noon(at)ntlworld(dot)com (David W Noon)
=======================================================================

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Gary Stainburn 2009-09-28 13:43:53 Re: simple (?) join
Previous Message Oliveiros C, 2009-09-28 11:14:03 Re: simple (?) join