0

I would like to understand the principle of how to update a BigQuery Nested table.

For example, with have a table:

ID   GROSS   OL.ID  OL.GROSS   PL.ID   PL.GROSS
1    100     12     200        34      454
             34     465        56      5667
             67     7876    

And I would like to update the gross value for the main, OL. and PL. to any other value

What is the way to do it?

2 Answers 2

2

Below is for BigQuery Standard SQL and extends Elliott's answer

Usually, there are more than just two elements in struct and you need to update just one or two - instead of explicitly calling them all out to rebuild original array - you can use below approach

UPDATE `project.dataset.table` SET
  OL = ARRAY(SELECT AS STRUCT * REPLACE(1.1 * GROSS AS GROSS) FROM UNNEST(OL)),
  PL = ARRAY(SELECT AS STRUCT * REPLACE(1.2 * GROSS AS GROSS) FROM UNNEST(PL))
Sign up to request clarification or add additional context in comments.

Comments

1

I'm assuming that OL and PL are arrays in this example but it's hard to tell. You can use a query like this:

UPDATE dataset.table
SET
  OL = ARRAY(SELECT AS STRUCT ID + 2 AS ID, GROSS + 3 AS GROSS FROM UNNEST(OL)),
  PL = ARRAY(SELECT AS STRUCT 1 AS ID, GROSS + 3 AS GROSS FROM UNNEST(PL))

See also the UPDATE repeated records example in the documentation

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.