Thread: retrieving last 3 rows

retrieving last 3 rows

From
soundar rajan
Date:
Hi all,

I need to retrieve the last n rows from a table.  In
Oracle, I write it as

select * from T_name where rowid > (select max(rowid)
from T_name) order by PK;

In pg, I wrote the same with needed casting as,

select * from T_name where int8(oid) > (select
max(int8(oid)) from T_name) order by PK;

The problem is, the oid returned is not in an order,
it's not consecutive, in which case, the no. of rows
to be retrieved is not predictable.

the sample oid in my table is 19682,19744....

Can any one help me in getting the same.

Thanks in advance.



__________________________________________________
Do You Yahoo!?
Bid and sell for free at http://auctions.yahoo.com

Re: [GENERAL] retrieving last 3 rows

From
"Aaron J. Seigo"
Date:
hi...

> In pg, I wrote the same with needed casting as,
>
> select * from T_name where int8(oid) > (select
> max(int8(oid)) from T_name) order by PK;

an easy way is to do this:

select * from T-name order by oid desc, PK limit 3;

this will not return them in PK order, but by order of writing... if PK is
incremented every record, they will appear in reverse order...

hope this helps...

--
Aaron J. Seigo
Sys Admin