共用方式為


OFFSET 子句

適用於:已勾選為「是」 Databricks SQL 已勾選為「是」 Databricks Runtime 11.3 LTS 和更新版本

略過語句或子查詢所傳回的數據列數目。 這個子句主要與 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