Google BigQuery联接问题查询失败错误:ON子句

时间:2019-02-20 14:09:05

标签: google-bigquery self-join

查询失败 错误:ON子句必须为AND of =每个表中一个字段名称的比较,所有字段名称都以表名称为前缀。考虑使用标准SQL .google.com / bigquery / docs / reference / standard-sql /),它允许非等分JOIN和涉及表达式和残差谓词的比较。

删除最后一行,此SQL在SQL Anywhere / ANSI上正常工作。 该如何解决?

SELECT b.number, b.state 
FROM [bigquery-public-data:usa_names.usa_1910_2013] as a 
join [bigquery-public-data:usa_names.usa_1910_2013] as b
on a.state = b.state 
and b.state = 'WY'

2 个答案:

答案 0 :(得分:0)

您可以将查询重写为

SELECT b.number, b.state 
FROM [bigquery-public-data:usa_names.usa_1910_2013] as a 
join 
(select number, state from [bigquery-public-data:usa_names.usa_1910_2013] where state='WY') as b
on a.state = b.state 

看来on子句的所有条件必须在两个表中至少有1列

答案 1 :(得分:0)

您面临的限制特定于BigQuery旧版SQL方言
强烈建议Migrate to BigQuery Standard SQL“方言”

以下内容适用于BigQuery Standard SQL,并且按预期工作

#standardSQL
SELECT b.number, b.state 
FROM `bigquery-public-data.usa_names.usa_1910_2013` AS a 
JOIN `bigquery-public-data.usa_names.usa_1910_2013` AS b
ON a.state = b.state 
AND b.state = 'WY'