BigQuery 中是否有与标准 SQL 等效的表通配符函数? [英] Is there an equivalent of table wildcard functions in BigQuery with standard SQL?

查看:21
本文介绍了BigQuery 中是否有与标准 SQL 等效的表通配符函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在旧版 SQL 中,用户可以使用表通配符函数,例如 TABLE_DATE_RANGETABLE_QUERYTABLE_DATE_RANGE_STRICT.

In legacy SQL, users can use table wildcard functions like TABLE_DATE_RANGE, TABLE_QUERY and TABLE_DATE_RANGE_STRICT.

是否有与标准 SQL 类似的功能?

Is there a similar feature with standard SQL?

推荐答案

在旧版 SQL 中,用户可以使用表通配符函数从数据集中的表子集中引用数据.在标准 SQL 中,用户可以使用 UNION ALL 获得相同的结果.但是,当用户希望使用例如日期范围(在旧 SQL 中支持使用 TABLE_DATE_RANGETABLE_DATE_RANGE_STRICT) 或其他复杂条件(由旧 SQL 中的 TABLE_QUERY 支持).对于标准 SQL,BigQuery 提供了与下面描述的等效的功能.

In legacy SQL, users can reference data from a subset of tables in a dataset using table wildcard functions. In standard SQL, users can achieve the same result using UNION ALL. However, this approach may not be convenient when users want to dynamically determine the set of tables using, for example, either a date range (supported using TABLE_DATE_RANGE and TABLE_DATE_RANGE_STRICT in legacy SQL) or other complex criteria (supported by TABLE_QUERY in legacy SQL). With Standard SQL, BigQuery offers an equivalent to this described below.

可以使用标准 SQL 重写以下使用 TABLE_QUERY 通配符函数的旧 SQL 查询.

The following legacy SQL query that uses the TABLE_QUERY wildcard function can be rewritten using standard SQL.

传统 SQL 查询(使用 TABLE_QUERY):

Legacy SQL query (using TABLE_QUERY):

SELECT SUM(value1)
FROM TABLE_QUERY([myproject:mydataset],"table_id = 'mydailytable_20150105' OR
table_id = 'mydailytable_20150106' OR table_id = 'maydailytable_20150110'")
GROUP BY value2;

旧版 SQL 查询(使用 TABLE_DATE_RANGE):

Legacy SQL query (using TABLE_DATE_RANGE):

SELECT SUM(value1)
FROM TABLE_DATE_RANGE([myproject:mydataset], TIMESTAMP("2015-01-05"), TIMESTAMP("2015-01-10"))

标准 SQL 查询:

SELECT SUM(value1)
FROM `myproject.mydataset.mydailytable_*`
WHERE _TABLE_SUFFIX = '20150105'
  OR _TABLE_SUFFIX = '20150106'
  OR _TABLE_SUFFIX = '20150110'
GROUP BY value2;

在上述查询中,通配符表 myproject.mydataset.mydailytable_* 匹配数据集 myproject.mydataset 中具有 table_id 的所有表从 mydailytable_ 开始.例如,要匹配数据集中的所有表,用户可以使用空前缀作为通配符.因此,myproject.mydataset.* 匹配数据集中的所有表.

In the above query, the wildcard table myproject.mydataset.mydailytable_* matches all tables in the dataset myproject.mydataset that have table_id starting with mydailytable_. For example, to match all tables in the dataset the user can use an empty prefix for the wildcard. So, myproject.mydataset.* matches all tables in the dataset.

由于 * 是一个特殊字符,所以在查询中使用通配符表名时必须引用它们.

Since * is a special character, wildcard table names must be quoted when using them in a query.

_TABLE_SUFFIX 伪列:

The _TABLE_SUFFIX pseudo column:

_TABLE_SUFFIX 伪列的类型为 STRING,可以像任何其他列一样使用.它是一个保留的列名,因此在将其用作 SELECT 列表的一部分时需要为其添加别名.

The _TABLE_SUFFIX pseudo column has type STRING and can be used just like any other column. It is a reserved column name, so it needs to be aliased when using it as part of the SELECT list.

此功能的官方文档可在此处获得:

Official documentation for this feature is available here:

https://cloud.google.com/bigquery/docs/wildcard-tableshttps://cloud.google.com/bigquery/docs/querying-wildcard-tables

这篇关于BigQuery 中是否有与标准 SQL 等效的表通配符函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆