X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.2]) by svr1.postgresql.org (Postfix) with ESMTP id B8D8ED1B50D for ; Tue, 11 Nov 2003 21:24:14 +0000 (GMT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (neptune.hub.org [200.46.204.2]) (amavisd-new, port 10024) with ESMTP id 24139-09 for ; Tue, 11 Nov 2003 17:23:44 -0400 (AST) Received: from shire.ontko.com (shire.ontko.com [199.164.165.1]) by svr1.postgresql.org (Postfix) with ESMTP id B45F6D1B531 for ; Tue, 11 Nov 2003 17:23:41 -0400 (AST) Received: from nick (bilbo.ontko.com [199.164.165.101]) by shire.ontko.com (8.12.3/8.12.3/Debian-6.6) with SMTP id hABLNE5f031392; Tue, 11 Nov 2003 16:23:26 -0500 Reply-To: From: "Nick Fankhauser" To: "Eric Soroos" Cc: "PGSQL-SQL" Subject: Re: Is there a more elegant way to write this query?... Date: Tue, 11 Nov 2003 16:23:02 -0500 Message-ID: MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit X-Priority: 3 (Normal) X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook IMO, Build 9.0.2416 (9.0.2911.0) Importance: Normal X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165 In-Reply-To: <0F5B254A-13C6-11D8-ABDC-0003930F2A6C@soroos.net> X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200311/129 X-Sequence-Number: 15944 Hi Eric- Thanks for your suggestion. > An explain analyze would help. I'll do that (and move the question to the performance list) when I get to the performance question, but at this point, I'm just seeking some help in looking at this from a different angle- I couldn't figure out how to achieve the result I was after without using max(), which felt like a kludge. Your solution gave me the start I needed. -Nick > How about: > select > actor.actor_full_name, > actor.actor_id, > s1.ctCases, > s1.case_id, > case_data.case_public_id > from > actor inner join ( select actor_id, count(*) as ctCases, > max(case_id) > as case_id > from > actor_case_assignment group by actor_id) as s1 > on (actor.actor_id = s1.actor_id) > left outer join case_data using > (s1.case_id=case_data.case_id) > limit 1000;