Re: pgsql aggregate: conditional max

Поиск
Список
Период
Сортировка
От Jeffrey Melloy
Тема Re: pgsql aggregate: conditional max
Дата
Msg-id 4413B301.7070207@visualdistortion.org
обсуждение исходный текст
Ответ на pgsql aggregate: conditional max  (Weimao Ke <wke@indiana.edu>)
Ответы Re: pgsql aggregate: conditional max  (Michael Fuhr <mike@fuhr.org>)
Список pgsql-sql
Weimao Ke wrote:
> Hi,
>
> I need a special aggregation function. For instance, given the 
> following table data:
>
>   aid    |   cat   | weight
> ----------+---------+---------
> a1  | Drama   |       1
> a1  | Romance |       6
> a1  | Short   |       1
> a1 | Other   |       7
> a2  | Comedy  |       1
> a2 | Drama   |       2
> a3  | Drama   |       1
> a3 | Adult   |       2
> a3 | Comedy  |       1
> a3 | Other   |       1
>
> I want to group by "aid" and choose the category (i.e., "cat") with 
> the largest "weight":
>
> aid   |   max_weighted_cat
> ----+---------------------
> a1   |   Other
> a2   |   Drama
> a3   |   Adult
>
> Any ideas? Thank you! :)
>

Should be able to do this with a standard max() aggregate.

select aid, cat, max(weight)
from table
group by aid, cat;

Jeff


В списке pgsql-sql по дате отправления:

Предыдущее
От: Weimao Ke
Дата:
Сообщение: pgsql aggregate: conditional max
Следующее
От: Michael Fuhr
Дата:
Сообщение: Re: pgsql aggregate: conditional max