有没有办法在BigQuery Standard SQL中添加联接的tablename?

时间:2017-08-15 12:08:41

标签: google-bigquery

我想知道是否可以强制标准SQL添加连接的表名,这样我就可以从a和b中获取所有字段,其中a_和b_前置,而无需手动命名每个字段。 IE浏览器。我想做一个SELECT *,我不想生成每个字段名。

我知道如果你从旧版SQL切换到标准SQL,那么

SELECT *
FROM first_table a
JOIN second_table b
ON a.key = b.key

不会自动将a_和b_添加到每个输出变量,如此问题中所述:How to remove/avoid prepended tablename in bigquery?但我想知道是否可以修改该行为。

2 个答案:

答案 0 :(得分:2)

唯一的选择是使用点“分离”名称,即返回一个带有每边列的结构:

SELECT a, b
FROM first_table a
JOIN second_table b
ON a.key = b.key;

如果您只想要每个表中的某些列,则可以使用子选择:

SELECT a, b
FROM (SELECT x, y, key FROM first_table) a
JOIN (SELECT foo, bar, key FROM second_table) b
ON a.key = b.key;

答案 1 :(得分:1)

这个问题有点泛泛 - 所以我的回答有机会不解决PO的具体实际需求,但我认为在某些情况下考虑以下方法会很有意思

想法是拥有所需列名的表(模板表 - 它可以只是空),而不是将它作为第一个表与union真实查询一起使用

   

这样的事情:

#standardSQL
SELECT * FROM template WHERE NULL UNION ALL
SELECT * 
FROM first_table AS a
JOIN second_table AS b 
ON a.key = b.key

您可以使用以下虚拟表

尝试/测试此方法
#standardSQL
WITH first_table AS (
  SELECT 777 AS key, 1 AS x, 2 AS y, 3 AS z
),
second_table  AS (
  SELECT 777 AS key, 4 AS x, 5 AS y, 6 AS z
),
template AS (
  SELECT NULL AS a_key, NULL AS a_x, NULL AS a_y, NULL AS a_z, NULL AS b_key, NULL AS b_x, NULL AS b_y, NULL AS b_z
)
SELECT * FROM template WHERE NULL UNION ALL
SELECT * 
FROM first_table AS a
JOIN second_table AS b 
ON a.key = b.key

结果如下

a_key   a_x a_y a_z b_key   b_x b_y b_z  
777     1   2   3   777     4   5   6