JOIN子句上的Bigquery类型转换

时间:2016-10-19 21:14:16

标签: google-bigquery

我正在尝试在两列上加入两个表

 -- query to join two tables
SELECT
  *
FROM
  [raw.raw_sales] AS game_records
JOIN
  [facebook_aggregate.avg_aggregate] AS avg_aggregate
ON
  (game_records.Away_team = avg_aggregate.team_name)
  AND (game_records.game_date = avg_aggregate.time_update)

它为我提供了此错误Error: 10.30 - 10.56: Timestamp literal or explicit conversion to timestamp is required.,因为game_records.game_date的类型为STRING,avg_aggregate.time_update的类型为DATE。

但如果我在JOIN..ON ..子句中进行转换

 -- query to join two tables
SELECT
  *
FROM
  [raw.raw_sales] AS game_records
JOIN
  [facebook_aggregate.avg_aggregate] AS avg_aggregate
ON
  (game_records.Away_team = avg_aggregate.team_name)
  AND (DATE(game_records.game_date) = DATE(avg_aggregate.time_update))

它给了我这个错误: Error: ON clause must be AND of = comparisons of one field name from each table, with all field names prefixed with table name. .

有没有办法在不创建中间表的情况下执行此操作?谢谢!

2 个答案:

答案 0 :(得分:2)

尝试使用standard SQL(取消选中“显示选项”下的“使用旧版SQL”)。除了删除表名周围的括号外,您不需要做任何事情:

SELECT
  *
FROM
  raw.raw_sales AS game_records
JOIN
  facebook_aggregate.avg_aggregate AS avg_aggregate
ON
  game_records.Away_team = avg_aggregate.team_name
  AND game_records.game_date = avg_aggregate.time_update;

答案 1 :(得分:1)

BigQuery标准SQL(请参阅Enabling Standard SQL)对ON子句没有此限制。尝试在标准SQL中运行查询 正如Elliott所说 - 确保你没有在表引用周围使用方括号。在标准SQL中 - 当你需要转义特殊字符时 - 你应该使用反向标记

如果您将遵循上述指示,请检查Migrating from legacy SQL