How to choose the latest partition in BigQuery table?

后端 未结 7 967
暗喜
暗喜 2020-12-09 21:21

I am trying to select data from the latest partition in a date-partitioned BigQuery table, but the query still reads data from the whole table.

I\'ve tried (as far a

7条回答
  •  清歌不尽
    2020-12-09 22:07

    A compromise that manages to query only a few partitions without resorting to scripting or failing with missing partitions for fixed dates.

    WITH latest_partitions AS (
      SELECT *, _PARTITIONDATE AS date
      FROM `myproject.mydataset.mytable`
      WHERE _PARTITIONDATE > DATE_SUB(CURRENT_DATE(), INTERVAL 7 DAY)
    )
    SELECT
      *
    FROM
      latest_partitions
    WHERE
      date = (SELECT MAX(date) FROM latest_partitions)
    

提交回复
热议问题