Received: from maia.hub.org (maia-5.hub.org [200.46.204.29]) by mail.postgresql.org (Postfix) with ESMTP id D95981337BF9 for ; Fri, 14 Jan 2011 19:22:00 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.29]) (amavisd-maia, port 10024) with ESMTP id 91287-07 for ; Fri, 14 Jan 2011 23:21:54 +0000 (UTC) X-Greylist: delayed 00:15:00.834231 by SQLgrey-1.7.6 Received: from smtp2.peak6.com (smtp2.peak6.com [38.98.137.9]) by mail.postgresql.org (Postfix) with ESMTP id 669B11337BCB for ; Fri, 14 Jan 2011 19:21:50 -0400 (AST) X-AuditID: 0a02cb1e-b7bf8ae00000096d-bc-4d30d70823a6 Message-ID: <4D30D708.3020704@peak6.com> Date: Fri, 14 Jan 2011 17:06:48 -0600 From: Shaun Thomas Reply-To: sthomas@peak6.com Organization: Peak6 User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.7) Gecko/20100720 Lightning/1.0b2 Thunderbird/3.1.1 MIME-Version: 1.0 To: Nikolas Everett CC: "pgsql-performance@postgresql.org" Subject: Re: Best way to get the latest revision from a table References: In-Reply-To: Content-Type: text/plain; charset="ISO-8859-1"; format=flowed content-transfer-encoding: 7bit X-Brightmail-Tracker: AAAAAQAAAZE= X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.91 tagged_above=-10 required=5 tests=BAYES_00=-1.9, T_RP_MATCHES_RCVD=-0.01 X-Spam-Level: X-Archive-Number: 201101/112 X-Sequence-Number: 41899 On 01/14/2011 03:17 PM, Nikolas Everett wrote: > SELECT * > FROM request > JOIN (SELECT a, MAX(b) as b FROM test GROUP BY a) max USING (a) > JOIN test USING (a, b); This actually looks like a perfect candidate for DISTINCT ON. SELECT DISTINCT ON (a, b) a, b, revision FROM test ORDER BY a, b DESC; Maybe I'm just misunderstanding your situation, though. -- Shaun Thomas OptionsHouse | 141 W. Jackson Blvd. | Suite 800 | Chicago IL, 60604 312-676-8870 sthomas@peak6.com ______________________________________________ See http://www.peak6.com/email_disclaimer.php for terms and conditions related to this email