BigQuery加入计算字段

时间:2016-11-15 17:13:02

标签: sql join google-bigquery calculated-columns

我试图在计算字段上加入两个表:

SELECT
  CONCAT(year, mo, da) AS date,
  temp
FROM
  [bigquery-public-data:noaa_gsod.gsod2016] AS a
JOIN
  [bigquery-public-data:noaa_gsod.stations] AS b
ON
  a.stn = b.usaf
  AND a.wban = b.wban
JOIN
  [test-project:0000.ga_sessions_20161002] AS c
ON
  c.date = CONCAT(a.year, a.mo, a.da)
WHERE
  b.name = "DUBAI INTL"
ORDER BY
  a.year ASC,
  a.mo ASC,
  a.da ASC

我一直收到以下错误:

  

ON子句必须是AND = =每个字段名称的比较   表,所有字段名称都以表名为前缀

我认为这个错误是第二个ON语句的结果,但是我已经用它们的表名前缀了所有字段名。

我做错了什么?

3 个答案:

答案 0 :(得分:3)

您需要为查询启用standard SQL。完成后(取消选中"使用旧版SQL""显示选项")您可以运行例如:

SELECT
  CONCAT(year, mo, da) AS date,
  temp
FROM
  `bigquery-public-data.noaa_gsod.gsod2016` AS a
JOIN
  `bigquery-public-data.noaa_gsod.stations` AS b
ON
  a.stn = b.usaf
  AND a.wban = b.wban
JOIN
  `test-project.0000.ga_sessions_20161002` AS c
ON
  c.date = CONCAT(a.year, a.mo, a.da)
WHERE
  b.name = "DUBAI INTL"
ORDER BY
  a.year ASC,
  a.mo ASC,
  a.da ASC;

这是一个独立的例子,它也不依赖于ga_session表:

WITH MyTable AS (
  SELECT '20161003' AS date UNION ALL
  SELECT '20160830'
)
SELECT
  CONCAT(year, mo, da) AS date,
  temp
FROM
  `bigquery-public-data.noaa_gsod.gsod2016` AS a
JOIN
  `bigquery-public-data.noaa_gsod.stations` AS b
ON
  a.stn = b.usaf
  AND a.wban = b.wban
JOIN
  MyTable AS c
ON
  c.date = CONCAT(a.year, a.mo, a.da)
WHERE
  b.name = "DUBAI INTL"
ORDER BY
  a.year ASC,
  a.mo ASC,
  a.da ASC;

答案 1 :(得分:0)

尝试使用子查询:

FROM (SELECT a.*, date(CONCAT(a.year, a.mo, a.da)) as yyyymmdd
      FROM [bigquery-public-data:noaa_gsod.gsod2016] a
     ) a JOIN
     [bigquery-public-data:noaa_gsod.stations] b
     ON a.stn = b.usaf AND a.wban = b.wban JOIN
     [test-project:0000.ga_sessions_20161002] c
     ON c.date = a.yyyymmdd

或类似的东西。 。 。确切的比较可能取决于c.date的类型。

答案 2 :(得分:0)

默认情况下,您正在使用的JOIN是INNER JOIN,因此ON子句只能对列进行简单的相等。

要解决此问题,您需要使用带有WHERE子句的CROSS JOIN:

SELECT
  CONCAT(year, mo, da) AS date,
  temp
FROM
  [bigquery-public-data:noaa_gsod.gsod2016] AS a
JOIN
  [bigquery-public-data:noaa_gsod.stations] AS b
ON
  a.stn = b.usaf
  AND a.wban = b.wban
CROSS JOIN
  [test-project:0000.ga_sessions_20161002] AS c
WHERE
  b.name = "DUBAI INTL"
AND
  c.date = CONCAT(a.year, a.mo, a.da)
ORDER BY
  a.year ASC,
  a.mo ASC,
  a.da ASC