3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

BigQuery で月別に分割されたテーブルを指定の日付範囲でスキャンする

Last updated at Posted at 2015-02-26

次のようなフォーマットで月別にテーブルがあるとする。

nginx.access_201412
nginx.access_201501
nginx.access_201502
nginx.access_201503

2015-02-15 から 2015-03-03 までの範囲指定をしたいときは、料金節約のため、次のテーブルのみをスキャンしたい。

nginx.access_201502
nginx.access_201503

TABLE_DATE_RANGE は日付毎に分かれているテーブルにしか使えないため、TABLE_QUERY を駆使する。

SELECT
  *
FROM (
  TABLE_QUERY(nginx, "
    TIMESTAMP(
      REGEXP_EXTRACT(table_id, r'access_(\d{4})\d{2}') + '-' +
      REGEXP_EXTRACT(table_id, r'access_\d{4}(\d{2})') + '-01 00:00:00'
    ) <= TIMESTAMP('2015-03-03 00:00:00')
    AND
    TIMESTAMP(
      REGEXP_EXTRACT(table_id, r'access_(\d{4})\d{2}') + '-' + 
      REGEXP_EXTRACT(table_id, r'access_\d{4}(\d{2})') + '-01 00:00:00'
    ) >=
    TIMESTAMP(
      STRING(YEAR(TIMESTAMP('2015-02-15 00:00:00'))) + '-' + 
      STRING(MONTH(TIMESTAMP('2015-02-15 00:00:00'))) + '-01 00:00:00'
    )
"))
WHERE
  time >= '2015-02-15 00:00:00' AND time < '2015-03-03 00:00:00'
  ...

もっとスマートな方法があったら教えて下さい( ´∀`)

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?