Received: from localhost (unknown [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id BE151632D77 for ; Thu, 24 Sep 2009 18:21:19 -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 62024-05 for ; Thu, 24 Sep 2009 21:21:04 +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 0C3C46334EE for ; Thu, 24 Sep 2009 18:21:09 -0300 (ADT) Received: from dwnoon.ntlworld.com (localhost [127.0.0.1]) by karnak.local (Postfix) with ESMTP id 6CB841F003 for ; Thu, 24 Sep 2009 22:21:07 +0100 (BST) Date: Thu, 24 Sep 2009 22:21:06 +0100 From: David W Noon To: pgsql-sql@postgresql.org Subject: Re: simple (?) join Message-ID: <20090924222106.23a5c1f3@dwnoon.ntlworld.com> In-Reply-To: <4ABBD34B.9080600@emproshunts.com> References: <200909241616.36933.gary.stainburn@ringways.co.uk> <20090924194629.44bc236f@dwnoon.ntlworld.com> <4ABBD34B.9080600@emproshunts.com> 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.499 tagged_above=-10 required=5 tests=BAYES_00=-2.599, RDNS_DYNAMIC=0.1 X-Spam-Level: X-Archive-Number: 200909/121 X-Sequence-Number: 33186 On Thu, 24 Sep 2009 16:15:07 -0400, justin wrote about Re: [SQL] simple (?) join: > > >David W Noon wrote:On Thu, 24 Sep 2009 16:16:36 +0100, Gary Stainburn >wrote about [SQL] simple (?) join: > > 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? > >SELECT * FROM orders >WHERE o_id IN (SELECT o_id FROM orders_log > WHERE ol_timestamp = (SELECT MAX(ol_timestamp) FROM orders_log)); > >No joins required. > >I don't think that is what he is requesting. I read it he also wants >the timestamp included in the result set That shouldn't be too much of a problem. >A nested query > >Select > orders.*, > (SELECT MAX(ol_timestamp) FROM orders_log where orders_log.o_id = > orders.oid) From orders That won't give the desired results. I don't think the SQL parser will even accept it. >Still another option is using a join > >Select > orders.*, ol_timestamp > From orders > left join (SELECT MAX(ol_timestamp), o_id FROM orders_log group by > o_id) as JoinQuery on JoinQuery.o_id = orders.o_id That won't give the desired results either. If you change "left" to "inner" you will be closer though. Both of your queries will retrieve the entire orders table with a timestamp of some sort from the orders_log table. >The second one should be faster Try this for an elegant approach: WITH maxi AS (SELECT MAX(ol_timestamp) AS ts FROM orders_log) SELECT o.*, maxi.ts FROM orders AS o WHERE o.o_id IN (SELECT o_id FROM orders_log WHERE ol_timestamp = maxi.ts); This caches the latest timestamp in a temporary table so that it can both appear in the result set and be used to constrain the selection from the orders_log table. -- Regards, Dave [RLU #314465] ======================================================================= david.w.noon@ntlworld.com (David W Noon) =======================================================================