OFFSET 子句

适用于:check marked yes Databricks SQL SQL 仓库版本 2022.35 或更高版本 check marked yes Databricks Runtime 11.2 及更高版本

跳过语句或子查询返回的行数。 此子句大多与 LIMIT 结合使用,以对结果集进行分页,与 ORDER BY 结合使用以产生确定的结果。

注意

使用 LIMITOFFSET 对结果集进行分页时,跳过的行仍会得到处理。 这些行只是在结果集中被禁止显示。 对于资源密集型查询,不建议使用此技术进行分页。

语法

OFFSET integer_expression

参数

  • integer_expression

    一个返回整数的正文本表达式。

示例

> CREATE TEMP VIEW person (name, age)
    AS VALUES ('Zen Hui', 25),
              ('Anil B' , 18),
              ('Shone S', 16),
              ('Mike A' , 25),
              ('John A' , 18),
              ('Jack N' , 16);

-- Select the 4th and 5th rows by alphabetical order.
> SELECT name, age FROM person ORDER BY name LIMIT 2 OFFSET 3;
  Mike A  25
 Shone S  16

-- Specifying ALL option on LIMIT and an OFFSET of zero, returns all the rows.
> SELECT name, age FROM person ORDER BY name LIMIT ALL OFFSET 0;
  Anil B  18
  Jack N  16
  John A  18
  Mike A  25
 Shone S  16
 Zen Hui  25

-- A constant function expression as an input to OFFSET.
> SELECT name, age FROM person ORDER BY name OFFSET length('SPARK');
 Zen Hui  25

-- A non-literal expression as an input to OFFSET is not allowed.
> SELECT name, age FROM person ORDER BY name OFFSET length(name);
Error: The offset expression must evaluate to a constant value