Re: pgsql aggregate: conditional max

From: Weimao Ke <wke(at)indiana(dot)edu>
To: Daniel CAUNE <d(dot)caune(at)free(dot)fr>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: pgsql aggregate: conditional max
Date: 2006-03-12 19:59:19
Message-ID: 44147D97.5050301@indiana.edu
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Daniel CAUNE wrote:

> SELECT aid, cat
>
> FROM table, (
> SELECT aid, max(weight) as weight
> FROM table
> GROUP BY aid) AS tablemaxweight
> WHERE table.aid = tablemaxweight.aid
> AND table.weight = tablemaxweight.aid;
>
>There is a limit case you don't specify how to deal with, when two or more categories have the same maximum weight. The query I wrote retrieves all the categories that have the maximum weight, but perhaps you just want one per aid.
>
>
Yes, this will introduce duplicates. Yet it is not too difficult to
select only one for each aid from the results. Thank you!

Weimao

>--
>Daniel
>
>
>
>

--
All best,

Weimao

Weimao Ke
Indiana University Bloomington
School of Library and Information Science
http://ella.slis.indiana.edu/~wke

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Michael Glaesemann 2006-03-13 03:53:41 Re: Ask a PostgreSql question (about select )
Previous Message Robert Paulsen 2006-03-12 18:15:45 Re: Question re: relational technique