pgsql aggregate: conditional max

From: Weimao Ke <wke(at)indiana(dot)edu>
To: pgsql-sql(at)postgresql(dot)org
Subject: pgsql aggregate: conditional max
Date: 2006-03-12 05:09:48
Message-ID: 4413AD1C.9050604@indiana.edu
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

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! :)

--
All best,

Weimao

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

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Jeffrey Melloy 2006-03-12 05:34:57 Re: pgsql aggregate: conditional max
Previous Message Christopher Browne 2006-03-11 17:00:27 Re: Merging rows into one result?