pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Jeff Eckermann <jeff_eckermann@yahoo.com>
To: Gaetano Mendola <mendola@bigfoot.com>
To: pgsql-sql@postgresql.org
Subject: Re: Select the max on a field
Date: Thu, 12 Sep 2002 07:03:45 -0700 (PDT)
Message-ID: <20020912140346.6056.qmail@web20808.mail.yahoo.com> (raw)
In-Reply-To: <alq427$2ta2$1@news.hub.org>


--- 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



view thread (4+ messages)

Message-ID: <20020912140346.6056.qmail@web20808.mail.yahoo.com>
Permalink:  ../20020912140346.6056.qmail@web20808.mail.yahoo.com/
Also on:    postgresql.org/message-id/20020912140346.6056.qmail@web20808.mail.yahoo.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: jeff_eckermann@yahoo.com, mendola@bigfoot.com
  Subject: Re: Select the max on a field
  In-Reply-To: <20020912140346.6056.qmail@web20808.mail.yahoo.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox