Skip site navigation (1) Skip section navigation (2)

How to select rows that are the max for each subcategory?

From: "Kynn Jones" <kynnjo(at)gmail(dot)com>
To: "pgsql-general General" <pgsql-general(at)postgresql(dot)org>
Subject: How to select rows that are the max for each subcategory?
Date: 2008-09-25 18:01:17
Message-ID: c2350ba40809251101y81de3f8qb0e5daafeaad240f@mail.gmail.com (view raw or flat)
Thread:
Lists: pgsql-general
Suppose I have a table T that has, among its columns, the fields X and Y,
where Y is an integer, and multiple rows with the same value of X are
possible.  I want to select the rows corresponding to the greatest values of
Y for each value of X.  E.g. suppose that T is
X Y Z
a  1 eenie
a  3 meenie
a  2 miny
b  4 moe
b  0 catch

...the result of the desired query would be

a  3 meenie
b  4 moe

TIA!

Kynnjo

Responses

pgsql-general by date

Next:From: Harald Armin MassaDate: 2008-09-25 18:05:49
Subject: Re: How to select rows that are the max for each subcategory?
Previous:From: Pavel StehuleDate: 2008-09-25 17:58:07
Subject: Re: Dynamically created cursors vanish in PLPgSQL

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group