Re: [HACKERS] DISTINCT ON: speak now or forever hold your peace

From: Chris Bitmead <chris(at)bitmead(dot)com>
To: pgsql-hackers(at)postgreSQL(dot)org
Cc: pgsql-sql(at)postgreSQL(dot)org
Subject: Re: [HACKERS] DISTINCT ON: speak now or forever hold your peace
Date: 2000-01-25 03:09:50
Message-ID: 388D13FE.4E75CA05@bitmead.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers pgsql-sql

Philip Warner wrote:
> >I don't feel overly strongly about this, but if I remember right you can
> >do some pretty cool things with this feature, provided you do define
> >some semantics clearly. Like I think you can find the first tuple
> >(given some ORDER BY clause) that fulfills some criteria. I think it is
> >
> >SELECT DISTINCT ON name name, age ORDER BY age;
> >
> >will get the youngest person. This might not be clearly specified now,
> >but
> >as long as it's useful, how about clearly defining it? I don't know that
> >there is an easy way of doing this in standard SQL.
>
> I don't know about PGSQL, but in other systems, I use:
>
> Select <whatever> from <wherever> order by age asc limit to 1 row;
>
> I *think* the PGSQL syntax is:
>
> Select <whatever> from <wherever> order by age asc limit 1;

I think what I really meant was...

SELECT DISTINCT ON firstname firstname, age ORDER BY age.

Which would find the youngest person called "fred", the youngest person
called "paul", the youngest person called "jim" etc etc. which your
limit example wouldn't do.

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Tom Lane 2000-01-25 03:18:29 Re: [HACKERS] Happy column dropping
Previous Message Bruce Momjian 2000-01-25 03:09:42 Re: [HACKERS] DISTINCT ON: speak now or forever hold your peace

Browse pgsql-sql by date

  From Date Subject
Next Message Tom Lane 2000-01-25 03:45:49 Re: [HACKERS] DISTINCT ON: speak now or forever hold your peace
Previous Message Bruce Momjian 2000-01-25 03:09:42 Re: [HACKERS] DISTINCT ON: speak now or forever hold your peace