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

前端 未结 2 1712
有刺的猬
有刺的猬 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:30

    You can transform you update statement into below

    #standardSQL
    UPDATE 
      `project.dataset.mytable` mytable 
      SET value = new_value 
    FROM (
      WITH a AS
      (
        SELECT 1 AS id, 123.456 AS value
      )  
      SELECT a1.id a1_id, a2.id a2_id, 
        COALESCE(a1.value, a2.value) new_value
      FROM 
        a AS a1, 
        a AS a2 
    )
    WHERE 
      a1_id = mytable.id 
    OR 
      a2_id = mytable.id2
    

提交回复
热议问题