Subject Re: [firebird-support] whats the opposite of first?
Author Milan Babuskov
martinknappe wrote:
> when i want to select the first 5 entries from a table i do something
> like:
>
> select first 5 field from table order by field ascending
>
> now, what do i do if i want to select the LAST 5 ? (but keeping the
> same order)
>
> select last 5 field from table order by field ascending
>
> doesnt work (fb 1.5)

Provided that you don't have two fields with same value:

select field
from table t1
where 5 > ( select count(*) from table t2 where t2.field < t1.field )
order by field descending;


If there is index on the "field", this should work fast.

--
Milan Babuskov
http://swoes.blogspot.com/
http://www.flamerobin.org