X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 7E7DE5E4721 for ; Thu, 2 Sep 2004 12:03:17 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 23342-09 for ; Thu, 2 Sep 2004 15:03:22 +0000 (GMT) Received: from news.hub.org (news.hub.org [200.46.204.72]) by svr1.postgresql.org (Postfix) with ESMTP id B0A335E471A for ; Thu, 2 Sep 2004 12:03:14 -0300 (ADT) Received: from news.hub.org (news.hub.org [200.46.204.72]) by news.hub.org (8.12.9/8.12.9) with ESMTP id i82F3LLq039992 for ; Thu, 2 Sep 2004 15:03:21 GMT (envelope-from news@news.hub.org) Received: (from news@localhost) by news.hub.org (8.12.9/8.12.9/Submit) id i82EdVIk089322 for pgsql-sql@postgresql.org; Thu, 2 Sep 2004 14:39:31 GMT From: Jeff Boes User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7.2) Gecko/20040803 X-Accept-Language: en-us, en MIME-Version: 1.0 X-Newsgroups: comp.databases.postgresql.sql Subject: Re: Complicated "group by" question References: In-Reply-To: Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit Lines: 38 Message-ID: X-Abuse-Report: http://www.usenetabuse.com Date: Thu, 02 Sep 2004 10:39:36 -0400 To: pgsql-sql@postgresql.org X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.0 tagged_above=0.0 required=5.0 tests= X-Spam-Level: X-Archive-Number: 200409/29 X-Sequence-Number: 18875 Andrew Perrin wrote: > I have a table of people ("reviewers"), a table of review assignments > ("assign"), and a table of review acceptances ("accept"). I would like to > be able to write a query to return the latest (e.g., max(assign_date)) > assignment for each reviewer, plus the acc_id field from "accept". I > think I should be able to do this with a GROUP BY clause, but am having no > luck. > > Table structure: > > reviewers assign accept > ----------------------------------------- > reviewer_id assign_id accept_id > reviewer_id assign_id > ... assign_date > ... ... > I think you want to write a non-GROUPed query using "DISTINCT ON". Something like this: SELECT DISTINCT ON (reviewer_id,assign_id) reviewer_id, assign_id, assign_date, accept_id FROM reviewers JOIN assign USING (reviewer_id) JOIN accept USING (accept_id) ORDER BY reviewer_id, assign_id, assign_date DESC; -- (Posted from an account used as a SPAM dump. If you really want to get in touch with me, dump the 'jboes' and substitute 'mur'.) ________ Jeffery Boes <>< jboes@qtm.net