Oracle: SELECT top N rows skipping the first X rows

URL dieses Beitrags: http://blog.stefan-macke.com/2012/07/19/oracle-select-top-n-rows-skipping-the-first-x-rows/

Apparently, it is not possible to use Oracle’s pseudo column rownum to select rows after a given number of rows, e.g. like

  1. SELECT * FROM tab WHERE rownum > 100;

or in MySQL

  1. SELECT * FROM tab LIMIT 100,50;

Oracle’s explanation for this (from the DB reference: ROWNUM):

The first row fetched is assigned a ROWNUM of 1 and makes the condition false. The second row to be fetched is now the first row and is also assigned a ROWNUM of 1 and makes the condition false. All rows subsequently fail to satisfy the condition, so no rows are returned.

However, to skip the first X rows in your query, you can make it a subquery like this (be sure to alias rownum!):

  1. SELECT * FROM
  2.     (SELECT field1, field2, ROWNUM rn FROM tab)
  3. WHERE rn > 100;

Einen Kommentar schreiben

XHTML: Diese Tags sind erlaubt: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>