简体   繁体   中英

How to select from variable table name?

I have a scheduled query set up, but I want to select FROM my_project.my_database.my_table_{todays_date} each day.

I found how to create variables in BigQuery like:

DECLARE todays_date STRING DEFAULT REPLACE(CAST(CURRENT_DATE AS STRING), '-', '') .

(Date Format: YYYYMMDD (no underscore or hyphen))

But how could I query from this table each day?

`my_project.my_database.my_table_{todays_date}`  

You can achieve this using queries with wildcards in the table name. The documentation here explains it very well:https://cloud.google.com/bigquery/docs/querying-wildcard-tables

Additionally in your case if you wanted to filter against a subset of the tables you could do something like this where the _TABLE_SUFFIX psuedo column is used to filter to select tables based on the @run_date variable if being done through a scheduled query:

SELECT *
FROM my_project.my_dataset.my_table_*
WHERE _TABLE_SUFFIX =  CAST(@run_date AS STRING)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM