Skip to main content
added 760 characters in body
Source Link
user1822
user1822

Using rownum without an order by will give you random results each time you run your query. To do a proper paging, you need to put this into a derived table together with an order by

select *
from (
  select foo.*
  from foo
  order by some_column -- this is important!!
) t
where rownum between 101 and 200;

For more details please read the explanation on rownum works in the manual http://docs.oracle.com/cd/E11882_01/server.112/e41084/pseudocolumns009.htm#SQLRF00255

For a very good discussion on how to do paging efficiently in Oracle (and other DBMS) I can highly recommend this site: http://use-the-index-luke.com/sql/partial-results/fetch-next-page

Edit after revealing the real statement:

My answer doesn't really change: you are not applying an order by in the inner query.

Your current query looks like this:

SELECT *
FROM (
  SELECT v.*,
         ROWNUM recnum
  FROM (
     ....
  ) v
)
WHERE ROWNUM BETWEEN 101 AND 200;

So there is no order by applied to the statement where the rownum is generated.

You need to apply the order by when selecting from derived table named v not inside it (and you don't really need the rownum as recnum in the inner query either)

SELECT *
FROM (
  SELECT *
  FROM (
     ....
  ) v
  ORDER BY lastupdate ASC
)
WHERE ROWNUM BETWEEN 101 AND 200;

Using rownum without an order by will give you random results each time you run your query. To do a proper paging, you need to put this into a derived table together with an order by

select *
from (
  select foo.*
  from foo
  order by some_column -- this is important!!
) t
where rownum between 101 and 200;

For more details please read the explanation on rownum works in the manual http://docs.oracle.com/cd/E11882_01/server.112/e41084/pseudocolumns009.htm#SQLRF00255

For a very good discussion on how to do paging efficiently in Oracle (and other DBMS) I can highly recommend this site: http://use-the-index-luke.com/sql/partial-results/fetch-next-page

Using rownum without an order by will give you random results each time you run your query. To do a proper paging, you need to put this into a derived table together with an order by

select *
from (
  select foo.*
  from foo
  order by some_column -- this is important!!
) t
where rownum between 101 and 200;

For more details please read the explanation on rownum works in the manual http://docs.oracle.com/cd/E11882_01/server.112/e41084/pseudocolumns009.htm#SQLRF00255

For a very good discussion on how to do paging efficiently in Oracle (and other DBMS) I can highly recommend this site: http://use-the-index-luke.com/sql/partial-results/fetch-next-page

Edit after revealing the real statement:

My answer doesn't really change: you are not applying an order by in the inner query.

Your current query looks like this:

SELECT *
FROM (
  SELECT v.*,
         ROWNUM recnum
  FROM (
     ....
  ) v
)
WHERE ROWNUM BETWEEN 101 AND 200;

So there is no order by applied to the statement where the rownum is generated.

You need to apply the order by when selecting from derived table named v not inside it (and you don't really need the rownum as recnum in the inner query either)

SELECT *
FROM (
  SELECT *
  FROM (
     ....
  ) v
  ORDER BY lastupdate ASC
)
WHERE ROWNUM BETWEEN 101 AND 200;
Source Link
user1822
user1822

Using rownum without an order by will give you random results each time you run your query. To do a proper paging, you need to put this into a derived table together with an order by

select *
from (
  select foo.*
  from foo
  order by some_column -- this is important!!
) t
where rownum between 101 and 200;

For more details please read the explanation on rownum works in the manual http://docs.oracle.com/cd/E11882_01/server.112/e41084/pseudocolumns009.htm#SQLRF00255

For a very good discussion on how to do paging efficiently in Oracle (and other DBMS) I can highly recommend this site: http://use-the-index-luke.com/sql/partial-results/fetch-next-page