Update a nested field in BigQuery using another nested field as a condition

纵饮孤独 提交于 2021-02-10 15:52:43

问题


I am trying to update the sourcePropertyDisplayName on a ga_sessions_ table WHERE it matches the value of another nested field. I found this answer here:

Update nested field in BigQuery table

But this only has a very simple WHERE TRUE; whereas I only want to apply it if it matches a specified hits.eventInfo.eventCategory.

Here is what I have so far:

UPDATE `dataset_name`.`ga_sessions_20170720`
SET hits =
  ARRAY(
    SELECT AS STRUCT * REPLACE(
      (SELECT AS STRUCT sourcePropertyInfo.* REPLACE('updated text' AS 
       sourcePropertyDisplayName)) AS sourcePropertyInfo)
    FROM UNNEST(hits)
  )
WHERE ARRAY(
SELECT AS STRUCT eventInfo.eventCategory
FROM UNNEST(hits)
) LIKE '%SEARCH%'

But I'm currently getting following error:

Error: No matching signature for operator LIKE for argument types: 
ARRAY<STRUCT<eventCategory STRING>>, STRING. Supported signatures: STRING 
LIKE STRING; BYTES LIKE BYTES at [8:7]

How can I update one nested field by using the value of another in a WHERE clause?


回答1:


Your WHERE clause should be like below

WHERE EXISTS (
  SELECT 1 FROM UNNEST(hits) AS h 
  WHERE h.eventInfo.eventCategory LIKE '%SEARCH%'
)


来源:https://stackoverflow.com/questions/45241798/update-a-nested-field-in-bigquery-using-another-nested-field-as-a-condition

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!