Re: [Fwd: Re: no ORDER BY in subselects?]

From: Philip Warner <pjw(at)rhyme(dot)com(dot)au>
To: Frank Bax <fbax(at)execulink(dot)com>, pgsql-sql(at)postgresql(dot)org
Subject: Re: [Fwd: Re: no ORDER BY in subselects?]
Date: 2000-09-21 18:35:17
Message-ID: 3.0.5.32.20000922043517.02f093d0@mail.rhyme.com.au
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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 |/

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Jie Liang 2000-09-21 18:35:30 Re: Convert from Seconds-Since-Epoch to Timestamp
Previous Message Webb Sprague 2000-09-21 18:10:36 Convert from Seconds-Since-Epoch to Timestamp