tags:

views:

67

answers:

1

Hello guys, here is a simple problem. I have a table of 500 rows and what to be able to select a given row number n. This is what I am doing:

    select *
    from table
    where table.arg1 ='A'
    and time_stamp=to_date('1/8/2010','MM/DD/YYYY')
    and rownum = n

But it would only work for the row 1, for the rest it doesn't return anything. Any idea?

+4  A: 

The reason why where rownum = 3 returns an empty rowset is that the condition is not true for the first row. For the second row, there is still no first row in the resultset, and rownum is still 1. So the condition fails again. See this page for a more detailed explanation.

You can use row_number() in a subquery:

select *
from (
    select row_number() over (order by col1) as rn, yt.* 
    from YourTable yt
) sub
where rn = 3

Or even simpler, but perhaps more confusing, using rownum itself:

select *
from (
    select rownum as rn, yt.* 
    from YourTable yt
) sub
where rn = 3
Andomar
Thanks Andomar it worked perfectly!! That forum is really good!