Re: pgsql aggregate: conditional max

From: Markus Schaber <schabi(at)logix-tt(dot)com>
To: Weimao Ke <wke(at)indiana(dot)edu>
Cc: Michael Fuhr <mike(at)fuhr(dot)org>, pgsql-sql(at)postgresql(dot)org
Subject: Re: pgsql aggregate: conditional max
Date: 2006-03-27 14:14:41
Message-ID: 4427F351.5010303@logix-tt.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Hi, Weimao Ke,

Weimao Ke wrote:
>> 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...:)

Try

SELECT DISTINCT aid, cat
FROM tablename AS t
JOIN (SELECT aid, max(weight) AS weight
FROM tablename
GROUP BY aid) AS s USING (aid, weight);

HTH,
Markus

--
Markus Schaber | Logical Tracking&Tracing International AG
Dipl. Inf. | Software Development GIS

Fight against software patents in EU! www.ffii.org www.nosoftwarepatents.org

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Markus Schaber 2006-03-27 14:25:55 Re: Permission to Select
Previous Message Markus Schaber 2006-03-27 14:00:35 Re: Problem using set-returning functions