Received: from maia.hub.org (unknown [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id 42B0563505E for ; Fri, 25 Sep 2009 00:07:24 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 95507-02 for ; Fri, 25 Sep 2009 03:07:16 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from images.kandalaya.org (etc.kandalaya.org [83.143.86.98]) by mail.postgresql.org (Postfix) with ESMTP id EA868634FF2 for ; Fri, 25 Sep 2009 00:07:15 -0300 (ADT) Received: from mail.linux-delhi.org (ABTS-North-Dynamic-036.116.161.122.airtelbroadband.in [122.161.116.36] (may be forged)) by images.kandalaya.org (8.14.3/8.14.3/Debian-9) with ESMTP id n8P379Re003118 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Fri, 25 Sep 2009 08:37:12 +0530 Received: from mail.linux-delhi.org (localhost [127.0.0.1]) by mail.linux-delhi.org (8.14.3/8.14.3/Debian-9) with ESMTP id n8P36mT6009560 for ; Fri, 25 Sep 2009 08:36:49 +0530 From: Raj Mathur Reply-To: raju@linux-delhi.org Organization: Kandalaya To: pgsql-sql@postgresql.org Subject: Re: simple (?) join Date: Fri, 25 Sep 2009 08:36:48 +0530 User-Agent: KMail/1.12.1 (Linux/2.6.30-1-686; KDE/4.3.1; i686; ; ) References: <200909241616.36933.gary.stainburn@ringways.co.uk> In-Reply-To: <200909241616.36933.gary.stainburn@ringways.co.uk> MIME-Version: 1.0 Content-Type: Text/Plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Message-Id: <200909250836.48639.raju@linux-delhi.org> X-Virus-Scanned: clamav-milter 0.95.2 at images.kandalaya.org X-Virus-Status: Clean X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=1.483 tagged_above=-10 required=5 tests=BAYES_50=0.001, DNS_FROM_RFC_BOGUSMX=1.482 X-Spam-Level: * X-Archive-Number: 200909/122 X-Sequence-Number: 33187 On Thursday 24 Sep 2009, Gary Stainburn wrote: > Hi folks. > > 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? Does this do what you want? You would have to do some tricks if you also want orders that don't have any entry in the orders_log table to be displayed. select ol.ol_id,ol.o_id,ol.ol_timestamp from orders o natural join orders_log ol where (ol.o_id,ol.ol_timestamp) in (select o_id,max(ol_timestamp) from orders_log group by o_id); Regards, -- Raju -- Raj Mathur raju@kandalaya.org http://kandalaya.org/ GPG: 78D4 FC67 367F 40E2 0DD5 0FEF C968 D0EF CC68 D17F PsyTrance & Chill: http://schizoid.in/ || It is the mind that moves