如何在BigQuery的标准SQL中编写LEFT JOIN?

时间:2016-12-26 13:07:23

标签: sql join google-bigquery standard-sql

我们有一个可以在BigQuery的Legacy SQL中运行的查询。我们如何在标准SQL中编写它以便它可以工作?

SELECT Hour, Average, L.Key AS Key FROM
(SELECT 1 AS Key, * 
FROM test.table_L AS L)
LEFT JOIN 
(SELECT 1 AS Key, Avg(Total) AS Average 
FROM test.table_R) AS R 
ON L.Key = R.Key ORDER BY Hour ASC

目前它给出的错误是:

Equality is not defined for arguments of type ARRAY<INT64> at [4:74]

BigQuery有两种查询模式:Legacy SQL和Standard SQL。我们已经查看了BigQuery Standard SQL documentation,并且还看到了关于BigQuery中标准SQL连接的one SO answer - 但到目前为止,我们还不清楚需要进行哪些关键更改。

Table_L如下所示:

Row    Hour
 1      A
 2      B
 3      C

Table_R如下所示:

Row    Value
 1      10
 2      20
 3      30

结果所需:

Row  Hour  Average(OfR)  Key
 1     A      20          1
 2     B      20          1 
 3     C      20          1

我们如何重写此BigQuery Legacy SQL查询以在标准SQL中工作?

2 个答案:

答案 0 :(得分:2)

您的错误消息表明key不是table_L中的列。如果不是,则不要将其包含在查询中。

看起来您只想要table_R的总和的平均值。你可以这样做:

SELECT l.*, r.average
FROM test.table_L as l CROSS JOIN
     (SELECT Avg(Total) as average 
      FROM test.table_R
     ) R 
ORDER BY l.hour ASC;

答案 1 :(得分:2)

根据您最近的问题和评论更新 - 请尝试以下

WITH Table_L AS (
SELECT 1 AS Row, 'A' AS Hour UNION ALL
SELECT 2 AS Row, 'B' AS Hour UNION ALL
SELECT 3 AS Row, 'C' AS Hour 
),
Table_R AS (
SELECT 1 AS Row, 10 AS Value UNION ALL
SELECT 2 AS Row, 20 AS Value UNION ALL
SELECT 3 AS Row, 30 AS Value 
)
SELECT 
  Row, 
  Hour, 
  (SELECT AVG(Value) FROM Table_R) AS AverageOfR,
  1 AS Key
FROM Table_L 

以上是测试

你应该在&#34;生产&#34;中运行的查询是

SELECT 
  Row, 
  Hour, 
  (SELECT AVG(Value) FROM Table_R) AS AverageOfR,
  1 AS Key
FROM Table_L 

如果由于某种原因您必须加入JOIN,请使用以下CROSS JOIN版本

SELECT 
  Row, 
  Hour, 
  AverageOfR,
  1 AS Key
FROM Table_L
CROSS JOIN ((SELECT AVG(Value) AS AverageOfR FROM Table_R))

或低于LEFT JOIN版本,其中涉及Key字段(如果Key对您的逻辑非常重要 - 我觉得这是真的)

SELECT 
  Row, 
  Hour, 
  AverageOfR,
  L.Key AS Key
FROM (SELECT 1 AS Key, Row, Hour FROM Table_L) AS L
LEFT JOIN ((SELECT 1 AS Key, AVG(Value) AS AverageOfR FROM Table_R)) AS R
ON L.Key = R.Key