Home > Net >  How to select from variable table name?
How to select from variable table name?

Time:08-27

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}`  

CodePudding user response:

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)
  • Related