【问题标题】:BigQuery - unsupported subquery with table in join predicateBigQuery - 连接谓词中的表不受支持的子查询
【发布时间】:2021-09-23 04:14:45
【问题描述】:

最近我开始研究 BigQuery,但有些事情仍然让我感到困惑。 Big Query 上此查询的替代方法是什么?

select a.abc, c.xyz
from table1 as a
left join table2 as c
on a.abc = c.abc
and c.date = (select t.date from table3 t)

问题是 Big Query 不支持 join 中的子查询。我尝试了许多替代方案,但结果彼此不匹配。

【问题讨论】:

  • 我相信它确实支持Standard SQL
  • @Tushar 我认为这不是因为我添加了#standardSQL 并且仍然出现错误
  • @hushhush 将您在 BQ 中运行的查询以及错误添加到问题中

标签: sql google-bigquery bigquery-udf


【解决方案1】:

假设table3.date 是唯一的,请尝试这样编写查询:

select a.abc, c.xyz
from table1 a left join
     (table2 c join
      table3 t
      on c.date = t.date
     )
     on a.abc = c.abc;

如果table3 中有重复项,您可以这样表述:

select a.abc, c.xyz
from table1 a left join
     (table2 c join
      (select distinct date
       from table3 t
      ) t
      on c.date = t.date
     )
     on a.abc = c.abc;

【讨论】:

    【解决方案2】:

    目前确实不支持连接谓词中的子查询。

    如果你真的需要这个,你可以file Feature Request。我相信很多人都会对此感到满意。

    有一个解决方法。你可以编写一个脚本,比如:

    declare date TIMESTAMP;
    set date = (select t.date from table3 t);
    
    select a.abc, c.xyz
    from table1 as a
    left join table2 as c
    on a.abc = c.abc
    and c.date = date;
    

    【讨论】:

      猜你喜欢
      • 2021-11-16
      • 2019-12-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-14
      • 1970-01-01
      • 2023-02-15
      • 2020-07-24
      相关资源
      最近更新 更多