##はじめに##
BigQueryでOffsetを指定する方法を紹介します。
公式のリファレンスにはOffsetに関する記載がありません。
https://cloud.google.com/bigquery/query-reference
##解決方法##
SELECT
repository.url
FROM
[publicdata:samples.github_nested]
WHERE
repository.url is NOT NULL
GROUP BY
repository.url
ORDER BY
repository.url
LIMIT 100
例えば上記のように100件ずつ取得したい場合
これだけで終了です。
SELECT
repository.url
FROM
[publicdata:samples.github_nested]
WHERE
repository.url is NOT NULL
GROUP BY
repository.url
ORDER BY
repository.url
LIMIT 100
OFFSET 100
そうですoffset
を普通に指定できるのです…
なんで公式のリファレンスには書いてないんでしょうね…
##検証##
以下は検証結果です。
SELECT
repository.url
FROM
[publicdata:samples.github_nested]
WHERE
repository.url is NOT NULL
GROUP BY
repository.url
ORDER BY
repository.url
limit 100
offset 0
の結果の100番目は
https://github.com/0x76/opensn0w
SELECT
repository.url
FROM
[publicdata:samples.github_nested]
WHERE
repository.url is NOT NULL
GROUP BY
repository.url
ORDER BY
repository.url
limit 100
offset 99
の1番目の結果が
https://github.com/0x76/opensn0w
ということでこの指定方法で問題なさそうです。
##追記##
6/16 追記
OFFSETが100万件を超え辺りで
Query Failed
Error: Response too large to return. Consider setting allowLargeResults to true in your job configuration. For more details, see https://cloud.google.com/bigquery/querying-data#largequeryresults
というエラーが出る場合があります。
そんな時は、必要がなくても
GROUP EACH BY
を利用し取得したいカラムをGROUP BY
することでエラーを回避できます。
関連書籍
BigQueryではじめるSQLデータ分析
SQL入門 Google BigQueryではじめるビジネスデータ分析