Merge data in tables
You can conditionally insert, update, or delete existing data in Hive tables using the ACID MERGE statement.
- Construct a query to update the customers' names and states in customer table to match the names and states of customers having the same IDs in the new_customer_stage table.
Enhance the query to insert data from new_customer_stage table into the customer table if none already exists.
MERGE INTO customer USING (SELECT * FROM new_customer_stage) sub ON sub.id = customer.id WHEN MATCHED THEN UPDATE SET name = sub.name, state = sub.new_state WHEN NOT MATCHED THEN INSERT VALUES (sub.id, sub.name, sub.state);