15 Aug 18 · Mike-Barn ·       Add to Favorites   Report

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

Read great educational content like this and a lot more !

Members get free exclusive access to content, new courses, and discounts. Signup for a free account to write a post / comment / upvote posts. Creating an account takes less than 5 seconds and you can start earning badges & points too

Copied