How do I get every nth row in a table, or how do I break up a subset of a table into sets or rows of equal size?

Jhe*_*ico 5 sql oracle

我有一个由主键 (ID) 和类型标识符 (TYPE_ID) 标识的异构数据表。我希望能够执行一个查询,该查询将给定类型的一组范围返回给偶数页面大小。例如,如果有 10,000 条类型为“1”的记录,并且我指定的页面大小为 1000,我需要 10 对数字返回表示我可以BETWEEN在后续查询中的子句中使用的值,以一次查询 DB 1000 条记录。

我最初的尝试是这样的

select id, rownum from CONTENT_TABLE 
where type_id = ? and mod(rownum, ?) = 0
Run Code Online (Sandbox Code Playgroud)

但这不起作用。

Ren*_*ger 5

rownum在评估where clause选择查询的之后,在返回所选记录时“评估” 。因此,您需要在另一个查询中对 rownum 进行选择。我给 r 作为 rownum 的别名:

select id from (
  select 
    id, 
    rownum r
  from 
    CONTENT_TABLE 
   where type_id = ? 
)
where mod(r, ?) = 0
Run Code Online (Sandbox Code Playgroud)