Thursday, September 25, 2008

Re: [GENERAL] How to select rows that are the max for each subcategory?

"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

--
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general

No comments: