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_stam开发者_如何学Pythonp=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?
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
精彩评论