BigQuery根据条件更新嵌套表

时间:2019-08-07 07:57:28

标签: google-bigquery

这是代码:

UPDATE `xxx.xxx.gift_test_1`
SET PaymentLines = ARRAY(SELECT AS STRUCT * REPLACE('gift_card' AS PaymentType) FROM UNNEST(PaymentLines) WHERE PaymentMethod = 'gift_card')
WHERE true

这里的问题是我只需要在PaymentMethod =到'gift_card'的情况下将Payment类型更新为'gift_card',但是我的代码在执行后将EVERYTHING和PaymentMethod和PaymentType都更新为'gift_card'

更新1.0

UPDATE `xxx.xxx.gift_test_1`
SET PaymentLines = ARRAY(
  SELECT AS STRUCT * REPLACE ('gift_card' AS PaymentType)
  FROM UNNEST(PaymentLines) as pay
)
WHERE EXISTS (select 1 from unnest(PaymentLines) as pay WHERE pay.PaymentMethod = 'gift_card')

此代码将所有PaymentType更新为“ gift_card”,因此在这种情况下WHERE子句不起作用。

UPDATE 2.0

我所拥有的: enter image description here

我需要: enter image description here

1 个答案:

答案 0 :(得分:1)

应该是

UPDATE `xxx.xxx.gift_test_1`
SET PaymentLines = ARRAY(
    SELECT AS STRUCT * 
      REPLACE(IF(PaymentMethod = 'gift_card', 'gift_card', PaymentType) AS PaymentType)
    FROM UNNEST(PaymentLines) AS pay
  )
WHERE true