简体   繁体   English

带有 CTE 的 BigQuery UPDATE 语句

[英]BigQuery UPDATE statement with CTEs

Looking for some help with BigQuery.寻求有关 BigQuery 的帮助。

I can't seem to use CTEs in UPDATE statements, ie:我似乎无法在 UPDATE 语句中使用 CTE,即:

with ctename as
(select
    column1,
    column2,
    column3,
from blah)


update table2 
set table2.column2 = ctename.column2
from table2
inner join ctename
    on ctename.column1 = table2.column1

I can't find any reason as to why this shouldn't work in BigQuery.我找不到任何理由说明为什么这在 BigQuery 中不起作用。 Any help/advice would be greatly appreciated.任何帮助/建议将不胜感激。

I don't think BigQuery supports CTEs in updates.我认为 BigQuery 在更新中不支持 CTE。 You can write this logic as:您可以将此逻辑编写为:

update table2 
    set table2.column2 = (select blah.column2 from blah where blah.column1 = table2.column1)
    where exists (select blah.column2 from blah where blah.column1 = table2.column1);

Looks like below is equivalent of what you are trying to achieve, while technically preserving your cte's query (that obviously can be much much more complex than just select * from the blah)看起来下面等同于您要实现的目标,同时在技术上保留您的 cte 查询(这显然比来自等等的 select * 复杂得多)

update table2 
set column2 = ctename.column2
from (
  select column1, column2, column3
  from blah
) ctename 
where ctename.column1 = table2.column1    

Note: as it is implicitly comes from your question and from other answer(s) - it is expected that that there is 1:1 matches by column1 - otherwise you will get error注意:因为它隐含来自您的问题和其他答案 - 预计 column1 有 1:1 匹配 - 否则您将收到错误

You can try with SELECT query and JOIN in CTE as follows:您可以尝试在 CTE 中使用SELECT查询和JOIN ,如下所示:

with cte as
(select
    blah.column1,
    blah.column2,
    blah.column3,
    table2.column2 as t2col2
from blah
inner join table2
on blah.column1 = table2.column1)
update cte 
set t2col2= column2;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM