Re: How to select rows that are the max for each subcategory? - Mailing list pgsql-general

From Tom Lane
Subject Re: How to select rows that are the max for each subcategory?
Date
Msg-id 9717.1222366355@sss.pgh.pa.us
Whole thread Raw
In response to How to select rows that are the max for each subcategory?  ("Kynn Jones" <kynnjo@gmail.com>)
Responses Re: How to select rows that are the max for each subcategory?  ("Kynn Jones" <kynnjo@gmail.com>)
List pgsql-general
"Kynn Jones" <kynnjo@gmail.com> writes:
> 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.

You could use DISTINCT ON --- see the "weather reports" example in the
SELECT reference page.  It's not standard SQL but will usually be faster
than solutions that use only standard features.

            regards, tom lane

pgsql-general by date:

Previous
From: Tom Lane
Date:
Subject: Re: Indirect access to NEW or OLD records
Next
From: Casey Allen Shobe
Date:
Subject: Re: Oracle and Postgresql