Re: [Fwd: Re: no ORDER BY in subselects?] - Mailing list pgsql-sql

From Philip Warner
Subject Re: [Fwd: Re: no ORDER BY in subselects?]
Date
Msg-id 3.0.5.32.20000922043517.02f093d0@mail.rhyme.com.au
Whole thread Raw
In response to Re: [Fwd: Re: no ORDER BY in subselects?]  (Frank Bax <fbax@execulink.com>)
List pgsql-sql
At 11:50 21/09/00 -0400, Frank Bax wrote:
>At 11:29 AM 9/21/00 +1000, you wrote:
>>The main reason I use them is to find the 'next' or 'previous' record in a
>>list (eg. next date, next ID). eg.
>>
>>  select <whatever>, (select ID from table where id > this.id 
>>  order by id asc limit 1) as next_id ...
>
>Doesn't this give the same result (without order by):
>
>>  select <whatever>, (select min(ID) from table where id > this.id) as
>next_id

Yes, but I don't think PostgreSQL is smart enough to use indexes to
evaluate the Min() function. 

Also, min/max does not work quite so well with a slightly more complex
example:

 select <whatever>, (select ID from table where date_field > this.date_field      order by date_field asc limit 1) as
next_id...
 

(ie. if the date_field and id are not correlated, but you want the id
corresponding to the next date).



----------------------------------------------------------------
Philip Warner                    |     __---_____
Albatross Consulting Pty. Ltd.   |----/       -  \
(A.B.N. 75 008 659 498)          |          /(@)   ______---_
Tel: (+61) 0500 83 82 81         |                 _________  \
Fax: (+61) 0500 83 82 82         |                 ___________ |
Http://www.rhyme.com.au          |                /           \|                                |    --________--
PGP key available upon request,  |  /
and from pgp5.ai.mit.edu:11371   |/


pgsql-sql by date:

Previous
From: "Brian C. Doyle"
Date:
Subject: Re: Multiple Index's
Next
From: Webb Sprague
Date:
Subject: Convert from Seconds-Since-Epoch to Timestamp