Oracle offset fetch 是可选的

Oracle offset fetch as optional


我有一个关于根据返回的行数使用偏移量的问题。我用的是 oracle 12c.

这是我过于简单化的查询示例:

SELECT COUNT(*) OVER () total, name, fullname
FROM person
ORDER BY name
offset 2 rows
fetch next 3 rows only

我想要实现的是仅当 total 大于 x(例如 100)时才进行偏移和提取

我已经试过了,但这不是一个有效的查询

SELECT COUNT(*) OVER () total, name, fullname
FROM person
ORDER BY name
CASE (total > 100) THEN (offset 2 rows
fetch next 3 rows only)

感谢

我想也许你应该改用 row_number():

WITH p AS (
      SELECT COUNT(*) OVER () as total, name, fullname,
             ROW_NUMBER() OVER (ORDER BY name) as seqnum
      FROM person p
     )
SELECT p.total, p.name, p.fullname
FROM p
WHERE (total > 100 AND seqnum BETWEEN 3 AND 5) OR
      (total <= 100 AND seqnum <= 3)
ORDER BY name ;