简体   繁体   English

如何生成包含今天日期的表名?

[英]How do I generate a table name that contains today's date?

It may seem a little strange, but there are already tables with names for each date.这可能看起来有点奇怪,但已经有每个日期名称的表格。

In my project, I have tables for each date to make statistics easier to handle.在我的项目中,我有每个日期的表格,以使统计数据更易于处理。

Of course, I don't think this is always the best way, but this is the table structure for my project.当然,我不认为这总是最好的方式,但这是我项目的表结构。 (It's a common technique in Google BigQuery and Amazon Athena. This question is about Google BigQuery) (这是 Google BigQuery 和 Amazon Athena 中的常用技术。这个问题是关于 Google BigQuery 的)

So to get the data, I want to generate today's date.所以为了获取数据,我想生成今天的日期。 If I use TODAY , I can get the data of the latest day without rewriting the code even if it is the next day.如果我使用TODAY ,即使是第二天,我也可以在不重写代码的情况下获取最近一天的数据。

I tried, but the code didn't work.我试过了,但代码不起作用。

Not work 1:不工作1:

CONCAT in FROM CONCATFROM

SELECT
  *
FROM
  CONCAT('foo_', FORMAT_TIMESTAMP('%Y%m%d', CURRENT_TIMESTAMP(), 'Asia/Tokyo'))

Error:错误:

Table-valued function not found: CONCAT at [4:3]未找到表值函数:CONCAT 在 [4:3]

Not work 2:不工作2:

create temporary function : create temporary function

create temporary function getTableName() as (CONCAT('foo_', FORMAT_TIMESTAMP('%Y%m%d', CURRENT_TIMESTAMP(), 'Asia/Tokyo')));

Error:错误:

CREATE TEMPORARY FUNCTION statements must be followed by an actual query. CREATE TEMPORARY FUNCTION 语句后面必须跟一个实际的查询。

Question

How do I generate a table name that contains TODAY 's date?如何生成包含TODAY日期的表名?

In this case, I would recommend you to useWild tables in BigQuery , which allows you to use some features in Standard SQL.在这种情况下,我建议您在 BigQuery 中使用Wild 表,它允许您使用标准 SQL 中的一些功能。

With Wild Tables you can use _TABLE_SUFFIX , it grants you the ability to filter/scan tables containing this parameter.通过 Wild Tables,您可以使用_TABLE_SUFFIX ,它使您能够过滤/扫描包含此参数的表。 The syntax would be as follows:语法如下:

SELECT *
FROM `test-proj-261014.sample.test_*` 
where _TABLE_SUFFIX = FORMAT_DATE('%Y%m%d', CURRENT_DATE)

I hope it helps.我希望它有帮助。

Your first query should go like this:你的第一个查询应该是这样的:

select CONCAT('foo_', FORMAT_TIMESTAMP('%Y%m%d', CURRENT_TIMESTAMP(), 'Asia/Tokyo'))

For creating temporary function, use the below code:要创建临时函数,请使用以下代码:

create temp function getTableName() as
((select CONCAT('foo_', FORMAT_TIMESTAMP('%Y%m%d', CURRENT_TIMESTAMP(), 'Asia/Tokyo'))
));
select getTableName()

The error "CREATE TEMPORARY FUNCTION statements must be followed by an actual query."错误“CREATE TEMPORARY FUNCTION 语句后面必须跟一个实际查询。” is because once the temporary functions are defined then you have to use the actual query to use that function and then the validity of function dies out.是因为一旦定义了临时函数,那么您必须使用实际查询来使用该函数,然后函数的有效性就会消失。 To define persistent UDFs and use them in multiple queries please go through the link to define permanent functions.You can reuse persistent UDFs across multiple queries, whereas you can only use temporary UDFs in a single query.要定义持久 UDF 并在多个查询中使用它们,请通过链接定义永久函数。您可以在多个查询中重用持久 UDF,而您只能在单个查询中使用临时 UDF。

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

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