Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

BigQuery – 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), '-', '').

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading