0

I have a table called sports as below

enter image description here

I need to append another day's details into the table for red team like below

enter image description here

Note: Without unnesting the existing record, just a simple append. Is there any way to get this done?

1 Answer 1

1

I am assuming that the column in question is an ARRAY<STRUCT>? Within a table, you can append new rows or you can update existing rows. For your puzzle, you can either delete the old row and append a new row containing your desired new data ... OR ... you can update the existing row providing the new value for the column you want changed. Since the value of the new column/cell is an ARRAY<STRUCT> then you must supply a new ARRAY<STRUCT>.

It would be something like:

UPDATE sports
SET details = ARRAY(UNNEST(details), (newDate, newScore))
WHERE team = red

I did see you asked "without unnesting the original record". One way me might be able to achieve that is by leveraging the ARRAY_CONCAT function. This takes two or more arrays and returns a new array that is the concatenation of the two. A possibility for the SQL might then be:

UPDATE sports
SET details = ARRAY_CONCAT(details, [(newDate, newScore)])
WHERE team = red

For this to work, we need to convert the new value you wish to append to the existing array to an array itself.

Sign up to request clarification or add additional context in comments.

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.