简体   繁体   中英

Update values in struct arrays in BigQuery

I am looking for an easy way to update values inside array of structs using SQL. Let's say we have a table:

CREATE TABLE schema.table
(
    date DATE,
    weights ARRAY<STRUCT<animal STRING, value FLOAT64>>
)
;

insert into schema.table
select cast('2020-01-01' as date), [('dog', 10.2), ('bird', 0.7), ('dragon', 3.2)]
union all
select cast('2020-01-02' as date), [('dog', 10.3), ('bird', 0.7)]
union all
select cast('2020-01-03' as date), [('dragon', 3.3)]

So the table looks like:

在此处输入图像描述

I would somehow like to update this table and change all dragon names to a cat .

Below is for BigQuery Standard SQL

#standardSQL
UPDATE `project.dataset.table` t
SET weights =  
  ARRAY(
    SELECT AS STRUCT IF(animal = 'dragon', 'cat', animal) animal, value
    FROM t.weights 
  ) 
WHERE TRUE

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM