Received: from localhost (unknown [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id 7EAA46331C0 for ; Mon, 28 Sep 2009 08:27:30 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by localhost (mx1.hub.org [200.46.208.211]) (amavisd-maia, port 10024) with ESMTP id 02036-07 for ; Mon, 28 Sep 2009 11:27:13 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from karnak.local (cpc3-lutn10-2-0-cust261.9-3.cable.virginmedia.com [81.97.85.6]) by mail.postgresql.org (Postfix) with ESMTP id A6C20632D9F for ; Mon, 28 Sep 2009 08:27:19 -0300 (ADT) Received: from dwnoon.ntlworld.com (localhost [127.0.0.1]) by karnak.local (Postfix) with ESMTP id 25F381F002 for ; Mon, 28 Sep 2009 12:27:17 +0100 (BST) Date: Mon, 28 Sep 2009 12:27:16 +0100 From: David W Noon To: pgsql-sql@postgresql.org Subject: Re: simple (?) join Message-ID: <20090928122716.61b99cfb@dwnoon.ntlworld.com> In-Reply-To: <200909281202.20768.gary.stainburn@ringways.co.uk> References: <200909241616.36933.gary.stainburn@ringways.co.uk> <20090926205349.5fa284f1@dwnoon.ntlworld.com> <4ABE7669.50001@emproshunts.com> <200909281202.20768.gary.stainburn@ringways.co.uk> X-Mailer: Claws Mail 3.7.2 (GTK+ 2.16.6; i686-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-2.034 tagged_above=-10 required=5 tests=AWL=0.465, BAYES_00=-2.599, RDNS_DYNAMIC=0.1 X-Spam-Level: X-Archive-Number: 200909/131 X-Sequence-Number: 33196 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.noon@ntlworld.com (David W Noon) =======================================================================