Google BigQuery - how to upsert into tables using MERGE Statement?

Problem: Insert row only if it does not exist, otherwise - update an existing row

Solution:

Use the merge function in BigQuery:

MERGE dataset.Inventory T
USING dataset.NewArrivals S
ON T.product = S.product
WHEN MATCHED THEN
UPDATE SET quantity = T.quantity + S.quantity
WHEN NOT MATCHED THEN
INSERT (product, quantity) VALUES(product, quantity)

Mike-Barn

posted on 15 Aug 18

Enjoy great content like this and a lot more !

Signup for a free account to write a post / comment / upvote posts. Its simple and takes less than 5 seconds