Re: Select the max on a field - Mailing list pgsql-sql

From Jeff Eckermann
Subject Re: Select the max on a field
Date
Msg-id 20020912140346.6056.qmail@web20808.mail.yahoo.com
Whole thread Raw
In response to Re: Select the max on a field  ("Gaetano Mendola" <mendola@bigfoot.com>)
List pgsql-sql
--- Gaetano Mendola <mendola@bigfoot.com> wrote:
> 
> "Gaetano Mendola" <mendola@bigfoot.com> wrote in
> message
> news:alq3mr$2s7o$1@news.hub.org...
> > Hi all,
> >
> > Suppose that I have a table like this:
> >
> >
> > att_1  |    att_2 |    att_3 |  att_4
> > --------------------------------
> > 1       |      a      |    y       |    y1
> > 2       |      b      |    y       |    y2
> > 3       |      a      |    xx     |    y3
> > 4       |      c      |    zz      |    y4
> > 5       |      a      |    t        |    y5
> > 6       |      c      |    x       |    y6
> >
> >
> >
> > I want obtain all row that have for each att_2
> > the max of att_1
> 
> I forgot to say: "with a single total query without
> sub select "
> 
If you don't mind a non-portable feature, DISTINCT ON
should do what you want.  Something like:

SELECT DISTINCT ON (att_2) att_1, att_2, att_3, att_4
FROM table
ORDER BY att_2, att_1 DESC;

__________________________________________________
Do you Yahoo!?
Yahoo! News - Today's headlines
http://news.yahoo.com


pgsql-sql by date:

Previous
From: Stephan Szabo
Date:
Subject: Re: Select the max on a field
Next
From: "Gaetano Mendola"
Date:
Subject: Performance inside and outside view ( WAS Re: Select the max on a field )