How to update table while joining on CTE results in BigQuery?

前端 未结 2 1749
有刺的猬
有刺的猬 2021-01-24 15:58

I think it will be obvious to see what I\'m trying to do through this simplified example (works in PostgreSQL)....

with a as
(
    select 1 as id, 123.456 as val         


        
2条回答
  •  情书的邮戳
    2021-01-24 16:34

    With BigQuery scripting (Beta now), CREATE TEMP TABLE can be an alternative way. See public documentation here.

    You should test to see the performance impact of using a temp table, which may be better or worse. As you can image, with scripting, it is possible to put multiple UPDATE query in one script and sharing a temp table a among them, which increases the opportunity of having better performance.

    CREATE TEMP TABLE a as
        select 1 as id, 123.456 as value;
    
    update 
        mytable 
        set value = coalesce(a1.value, a2.value) 
    from 
        a as a1, 
        a as a2 
    where 
        a1.id = mytable.id 
    or 
        a2.id = mytable.id2
    

提交回复
热议问题