Mahesh S wrote:
Well, the problem is, this table has say 1000 records or more.
When displaying the results on a web page, we are only displaying 25
entries per page and hence the result would be split into 1000/25
entries and there are options allowing the user to navigate to x*25 set
of results where x could be an entry number.
So, how do I randomnly jump to any subset of the results? Isnt there
any option like
select * from table fetch rows 50 to 100 only
or something similar.
Thanks
Mahesh
Probably the best way to do this, if you have an orderable column, is
to use an order by with fetch first x rows. So, you could select:
select * from table order by orderable_column fetch first 25 rows only
the keep the value of the orderable_column for the last row returned,
and when you want the next 25, issue:
select * from table where orderable_column 'keptvalue' order by
orderable_column fetch first 25 rows only
and so on. If you don't have a column that you want to order by (or
there isn't an appropriate column that works for some reason), then
using row_number is the next best option:
select {column list} from (select {column list}, row_number() over() as
row_num from table) x where row_num between 26 and 50
Note that {column list} needs to be a nice list of columns. Also, you
can put an order by clause in the over() clause if you want an ordering
to the results like over(order by col_a desc).
This (row_number()) works just fine, and is probably what you are
asking for - it just isn't quite as efficient as the other option.
-Chris