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 CE7E55E46D2 for ; Wed, 25 Aug 2004 14:53:14 -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 77526-01 for ; Wed, 25 Aug 2004 17:53:01 +0000 (GMT) Received: from simmts6-srv.bellnexxia.net (simmts6.bellnexxia.net [206.47.199.164]) by svr1.postgresql.org (Postfix) with ESMTP id 3EDF65E46CD for ; Wed, 25 Aug 2004 14:52:58 -0300 (ADT) Received: from localhost ([67.71.24.121]) by simmts6-srv.bellnexxia.net (InterMail vM.5.01.06.10 201-253-122-130-110-20040306) with ESMTP id <20040825175244.JSSD1635.simmts6-srv.bellnexxia.net@localhost>; Wed, 25 Aug 2004 13:52:44 -0400 Received: from [192.168.250.6] ([192.168.250.6]) by (8.11.6/8.11.6) with ESMTP id i7PHqtg20224; Wed, 25 Aug 2004 13:52:56 -0400 Message-ID: <412CD247.5020706@sympatico.ca> Date: Wed, 25 Aug 2004 13:54:15 -0400 From: Jean-Luc Lachance User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.7.2) Gecko/20040804 Netscape/7.2 (ax) X-Accept-Language: en, en-us, en-gb, fr, fr-ca MIME-Version: 1.0 To: Andrew Perrin Cc: pgsql-sql@postgresql.org Subject: Re: Complicated "group by" question References: In-Reply-To: Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit 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: 200408/266 X-Sequence-Number: 18763 Andrew, If assing is not a many to many relation, why did you not fold accept_id into assign? Any way, here is the query you need: select assign.reviewer_id, ss.max_assign_date, accept.assign_id, accept.accept_id from ( select reviewer_id, max( assign_date) as max_assign_date from assign group by reviewer_id) as ss, assign, accept where ss.reviewer_id = assign.reviewer_id and ss.max_assign_date = assign.assign_date and assign.assign_id = accept.assign_id; 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 > ... ... > > > Thanks for any guidance. > > Andy > > ---------------------------------------------------------------------- > Andrew J Perrin - http://www.unc.edu/~aperrin > Assistant Professor of Sociology, U of North Carolina, Chapel Hill > clists@perrin.socsci.unc.edu * andrew_perrin (at) unc.edu > > > ---------------------------(end of broadcast)--------------------------- > TIP 1: subscribe and unsubscribe commands go to majordomo@postgresql.org >