Google BigQuery-更新嵌套的收入字段

时间:2018-11-07 09:48:22

标签: google-analytics google-bigquery

我尝试将Google BigQuery - Updating a nested repeated field中的解决方案应用于字段hits.transaction.transactionRevenue,但收到错误消息:

  

标量子查询产生了多个元素

我尝试运行以下查询:

UPDATE `project_id.dataset_id.table`
SET hits = ARRAY(
  SELECT AS STRUCT * REPLACE (
    (SELECT AS STRUCT transaction.* REPLACE (1 AS transactionRevenue)) AS transaction
  )
  FROM UNNEST(hits) as transactionRevenue
)
WHERE (select h.transaction.transactionId from unnest(hits) as h) LIKE 'ABC123XYZ'

我有明显的错误吗?如果有人可以分享一些提示或经验来帮助我,那就太好了。

我基本上想要做的是将特定交易的收入设置为1。

非常感谢,

大卫

1 个答案:

答案 0 :(得分:0)

这是问题所在

WHERE (select h.transaction.transactionId from unnest(hits) as h) LIKE 'ABC123XYZ'

如果数组中有多个命中,这将导致您看到的错误。您可能想要这样:

WHERE EXISTS (select 1 from unnest(hits) as h WHERE h.transaction.transactionId LIKE 'ABC123XYZ')

但是请注意,对于此条件为真的任何行,您的UPDATE现在将替换数组的 all 元素。您可能想要的是改为在ARRAY函数调用中移动条件:

UPDATE `project_id.dataset_id.table`
SET hits = ARRAY(
  SELECT AS STRUCT * REPLACE (
    (SELECT AS STRUCT transaction.* REPLACE (1 AS transactionRevenue)) AS transaction
  )
  FROM UNNEST(hits) as h
  WHERE h.transaction.transactionId LIKE 'ABC123XYZ'
)
WHERE true

现在替换仅适用于交易ID与模式匹配的匹配。