Received: from localhost (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id 738854760CC for ; Thu, 12 Sep 2002 10:03:42 -0400 (EDT) Received: from web20808.mail.yahoo.com (web20808.mail.yahoo.com [216.136.226.197]) by postgresql.org (Postfix) with SMTP id BB8E24760A7 for ; Thu, 12 Sep 2002 10:03:41 -0400 (EDT) Message-ID: <20020912140346.6056.qmail@web20808.mail.yahoo.com> Received: from [208.13.35.65] by web20808.mail.yahoo.com via HTTP; Thu, 12 Sep 2002 07:03:45 PDT Date: Thu, 12 Sep 2002 07:03:45 -0700 (PDT) From: Jeff Eckermann Subject: Re: Select the max on a field To: Gaetano Mendola , pgsql-sql@postgresql.org In-Reply-To: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Virus-Scanned: by AMaViS new-20020517 X-Archive-Number: 200209/102 X-Sequence-Number: 9379 --- Gaetano Mendola wrote: > > "Gaetano Mendola" 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