Re: [HACKERS] DISTINCT ON: speak now or forever hold your peace - Mailing list pgsql-sql

From Chris Bitmead
Subject Re: [HACKERS] DISTINCT ON: speak now or forever hold your peace
Date
Msg-id 388D13FE.4E75CA05@bitmead.com
Whole thread Raw
In response to DISTINCT ON: speak now or forever hold your peace  (Tom Lane <tgl@sss.pgh.pa.us>)
Responses Re: [HACKERS] DISTINCT ON: speak now or forever hold your peace  (Peter Eisentraut <peter_e@gmx.net>)
List 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.


pgsql-sql by date:

Previous
From: Philip Warner
Date:
Subject: Re: [HACKERS] DISTINCT ON: speak now or forever hold your peace
Next
From: Bruce Momjian
Date:
Subject: Re: [HACKERS] DISTINCT ON: speak now or forever hold your peace