Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Updating a struct column in bigquery

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading