pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Brook Milligan <brook@trillium.NMSU.Edu>
Cc: pgsql-sql@postgreSQL.org
Subject: Re: [SQL] Finding the "most recent" rows
Date: Fri, 23 Apr 1999 12:29:28 -0400
Message-ID: <21889.924884968@sss.pgh.pa.us> (raw)
In-Reply-To: <199904231442.IAA17294@trillium.nmsu.edu>

Brook Milligan <brook@trillium.NMSU.Edu> writes:
>    Tom Lane wrote:
>> 
>> SELECT DISTINCT ON place * FROM table ORDER BY place, time DESC;
>> 
>> might do the right thing.  It *seems* to select the first row for each
>> value of place.  I've never seen a spec for this feature, however, so
>> I'm not sure if it's reliable or not...

> Cool idea.  Is DISTINCT guarranteed to choose the first row that
> matches or can it choose any row?

I dunno, that's why I said I was unsure that this method was reliable.
By experimentation it seems that Postgres' DISTINCT code works that way,
but I have no idea whether the SQL spec mandates it or allows any row
within a group to be chosen.

I recall now that when I first heard of "SELECT DISTINCT ON field"
I objected that the results weren't well-defined (since it's not clear
how DISTINCT will choose which tuple to return).  It might be that the
SQL spec requires the first tuple to be chosen for each value of
"field", which would allow the user to control the results by inserting
a preceding ORDER BY step --- or skip the ORDER BY, if he doesn't really
care which tuple he gets.  That'd actually be a pretty cool design.
Anyone have an SQL spec handy to check it?

			regards, tom lane



view thread (24+ messages)  latest in thread

Message-ID: <21889.924884968@sss.pgh.pa.us>
Permalink:  ../21889.924884968@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/21889.924884968@sss.pgh.pa.us

 · 

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 from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-sql@postgresql.org
  Cc: tgl@sss.pgh.pa.us, brook@trillium.NMSU.Edu
  Subject: Re: [SQL] Finding the "most recent" rows
  In-Reply-To: <21889.924884968@sss.pgh.pa.us>
  References: <199904231442.IAA17294@trillium.nmsu.edu> <21889.924884968@sss.pgh.pa.us>

* 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