Re: pgsql aggregate: conditional max - Mailing list pgsql-sql

From Markus Schaber
Subject Re: pgsql aggregate: conditional max
Date
Msg-id 4427F351.5010303@logix-tt.com
Whole thread Raw
In response to Re: pgsql aggregate: conditional max  (Weimao Ke <wke@indiana.edu>)
List 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


pgsql-sql by date:

Previous
From: Markus Schaber
Date:
Subject: Re: Problem using set-returning functions
Next
From: Markus Schaber
Date:
Subject: Re: Permission to Select