简体   繁体   English

Google BigQuery:创建表时将日期添加到表名

[英]Google BigQuery: Add date to table name when creating a table

I am writing a query I am planning to schedule using Big Query UI.我正在编写一个我计划使用 Big Query UI 安排的查询。

I would like to add a _TABLE_SUFFIX to this table which is equal to CURRENT_DATE.我想在此表中添加一个等于 CURRENT_DATE 的 _TABLE_SUFFIX。

How could I achieve that?我怎样才能做到这一点?

This is the query I am working on:这是我正在处理的查询:

IF 
  today != DATE_SUB(DATE_TRUNC(CURRENT_DATE(), MONTH), INTERVAL 1 DAY)
THEN 
  CREATE TABLE `project.dataset.tablename_<insert_current_date_here>`
  AS
  SELECT CURRENT_DATE() as today;
END IF;

The best bet would be to generate the query dynamically, and then execute it statically.最好的办法是动态生成查询,然后静态执行它。

This could be done using something like python.这可以使用 python 之类的东西来完成。

from datetime import datetime从日期时间导入日期时间

def get_query():
    return '''IF 
    today != DATE_SUB(DATE_TRUNC(CURRENT_DATE(), MONTH), INTERVAL 1 DAY)
    THEN 
    CREATE TABLE `project.dataset.%s`
    AS
    SELECT CURRENT_DATE() as today;
    END IF;''' % str(datetime.now())

BigQuery supports a template system for destination table names in scheduled queries. BigQuery 支持用于计划查询中的目标表名称的模板系统 To add the current date to the table name, use the provided template syntax.要将当前日期添加到表名,请使用提供的模板语法。 For example, tablename_{run_time|"%Y%m%d"} would output tablename_YYYYMMDD .例如, tablename_{run_time|"%Y%m%d"}将输出tablename_YYYYMMDD

bigquery 计划查询设置

You could (whether you should is another debate) create dynamic table names via BQ's SQL procedural language capability, specifically the EXECUTE IMMEDIATE statement.您可以(是否应该是另一个争论)通过 BQ 的SQL 过程语言功能创建动态表名,特别是EXECUTE IMMEDIATE语句。

eg例如

DECLARE today STRING DEFAULT STRING(DATE_SUB(DATE_TRUNC(CURRENT_DATE(), MONTH), INTERVAL 1 DAY));

EXECUTE IMMEDIATE format("""
CREATE TABLE `project.dataset.tablename_%s` AS
SELECT CURRENT_DATE() as today
""", today);

For more also see towardsdatascience.com/how-to-use-dynamic-sql-in-bigquery .有关更多信息,请参阅towardsdatascience.com/how-to-use-dynamic-sql-in-bigquery

Note you might also now get error location issues with EXECUTE IMMEDIATE , if so try changing/checking your Processing location in Query Settings , see here请注意,您现在可能还会遇到EXECUTE IMMEDIATE的错误位置问题,如果是这样,请尝试Processing location in Query Settings ,请参见此处

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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