X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by postgresql.org (Postfix) with ESMTP id 28C069DCC4C for ; Sun, 12 Mar 2006 13:15:28 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 52594-01 for ; Sun, 12 Mar 2006 13:15:26 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey- Received: from rockridge.uits.indiana.edu (rockridge.uits.indiana.edu [129.79.1.74]) by postgresql.org (Postfix) with ESMTP id C413C9DCAA7 for ; Sun, 12 Mar 2006 13:15:24 -0400 (AST) Received: from mail-relay.iu.edu (stjoseph.uits.indiana.edu [129.79.1.78]) by rockridge.uits.indiana.edu (8.12.10/8.12.10/IUPO) with ESMTP id k2CH7rEU011213; Sun, 12 Mar 2006 12:07:54 -0500 (EST) Received: from [192.168.0.3] (149-159-1-54.dhcp-bl.indiana.edu [149.159.1.54]) (authenticated bits=0) by mail-relay.iu.edu (8.12.10/8.12.10/IUPO) with ESMTP id k2CH7r7r025076; Sun, 12 Mar 2006 12:07:53 -0500 (EST) Message-ID: <44145569.3010208@indiana.edu> Date: Sun, 12 Mar 2006 12:07:53 -0500 From: Weimao Ke User-Agent: Mozilla Thunderbird 1.0.2 (Windows/20050317) X-Accept-Language: en-us, en MIME-Version: 1.0 To: Michael Fuhr CC: pgsql-sql@postgresql.org Subject: Re: pgsql aggregate: conditional max References: <4413AD1C.9050604@indiana.edu> <20060312054212.GA25423@winnie.fuhr.org> In-Reply-To: <20060312054212.GA25423@winnie.fuhr.org> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, score=0.479 required=5 tests=[DNS_FROM_RFC_ABUSE=0.479] X-Spam-Score: 0.479 X-Spam-Level: X-Archive-Number: 200603/175 X-Sequence-Number: 24521 Michael Fuhr wrote: >SELECT DISTINCT ON (aid) aid, cat >FROM tablename >ORDER BY aid, weight DESC, cat; > > Good pointer. I think this will solve my problem. :) >SELECT aid, cat >FROM tablename AS t >JOIN (SELECT aid, max(weight) AS weight > FROM tablename > GROUP BY aid) AS s USING (aid, weight); > > This query will return duplicates if there are multiple categories (for one aid) with the same max weight. Yet, I should be able to remove the duplicates somehow...:) I really appreciate your help! -- All best, Weimao Weimao Ke Indiana University Bloomington School of Library and Information Science http://ella.slis.indiana.edu/~wke