Reputation: 1708
I have a BigQuery
table named 'events' with below structure:
My goal is to update all rows where country is US and replace it with 'United States', and where country is CH to replace it with 'China'.
I tried to look at other questions where they update repeated record but I didn't find anything specific to what I want and didn't quite understood the method. (for example this StackOverflow question)
Any help would be much appreciated.
Upvotes: 0
Views: 494
Reputation: 12254
If you're trying to modify only two country codes into country names, following would be one of possible approach.
Note - below query is assuming that country code and region code can conflict each other.
UPDATE sample
SET attributes = ARRAY (
SELECT AS STRUCT a.name, IFNULL(c.name, a.value)
FROM UNNEST(attributes) a
LEFT JOIN UNNEST([
STRUCT('US' AS code, 'United States' AS name), ('CH', 'China')
]) c ON a = ('country', c.code)
)
WHERE ('country', 'US') IN UNNEST(attributes)
OR ('country', 'CH') IN UNNEST(attributes)
;
More general approach would be using a mapping table.
DECLARE countries ARRAY<STRUCT<code STRING, name STRING>> DEFAULT [
('US', 'United States'),
('CH', 'China') --, you can add more mappings of country code and it's name.
];
With the above mapping table, below query will generate same result as first query.
UPDATE sample
SET attributes = ARRAY(
SELECT AS STRUCT a.name, IFNULL(c.name, a.value)
FROM UNNEST(attributes) a
LEFT JOIN UNNEST(countries) c ON a = ('country', c.code)
)
WHERE TRUE
;
Upvotes: 2