在BigQuery中更新一个结构(struct)列

huangapple go评论85阅读模式
英文:

Updating a struct column in bigquery

问题

我有一个表格,其中包含1个普通模式列 id 和一个结构化(record, repeated)列 details
details 列内嵌了 countryname 列。

details 列内有五个记录。

country name
UK      sarah
IND     carter
Itay    marie
France  john
peru    christy

我正在使用以下查询来更新单个记录的值。

update 
info_table
set value=array(
    select as struct * replace('US' as country)) from unnest(details)
    where name='sarah'
)
where id=100
    

这将成功将国家值从英国(UK)更新为美国(US),但这将删除其他4条记录。

我需要一个查询来更新结构字段而不混淆其他记录。

英文:

I have a table with 1 normal mode column id and a struct(record, repeated) column details.
The details column has country and name column nested inside that.

I have five records inside the details column.

country name
UK      sarah
IND     carter
Itay    marie
France  john
peru    christy

I am using the below query to update the value of a single record.

update 
info_table
set value=array(
    select as struct * replace('US' as country)) from unnest(details)
    where name='sarah'
)
where id=100
    

This updates the country value from UK to US successfully, however, this does remove the rest of the 4 records.

I need a query to update the struct fields without messing up the other records.

答案1

得分: 1

UPDATE info_table
SET details = (
SELECT ARRAY(
SELECT AS STRUCT
IF(name = 'sarah', 'US', country) AS country,
name
FROM UNNEST(details)
)
)
WHERE id = 100

英文:

Try the following :

UPDATE info_table
SET details = (
  SELECT ARRAY(
    SELECT AS STRUCT
      IF(name = 'sarah', 'US', country) AS country,
      name
    FROM UNNEST(details)
  )
)
WHERE id = 100

huangapple
  • 本文由 发表于 2023年7月17日 21:26:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76704950.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定