在此示例中:https ://docs.databricks.com/_static/notebooks/merge-in-scd-type-2.html ,使用單個自然鍵來執行 MERGE INTO 邏輯,如下所示:
MERGE INTO customers
USING (
-- These rows will either UPDATE the current addresses of existing customers or INSERT the new addresses of new customers
SELECT updates.customerId as mergeKey, updates.*
FROM updates
UNION ALL
-- These rows will INSERT new addresses of existing customers
-- Setting the mergeKey to NULL forces these rows to NOT MATCH and be INSERTed.
SELECT NULL as mergeKey, updates.*
FROM updates JOIN customers
ON updates.customerid = customers.customerid
WHERE customers.current = true AND updates.address <> customers.address
) staged_updates
ON customers.customerId = mergeKey
WHEN MATCHED AND customers.current = true AND customers.address <> staged_updates.address THEN
UPDATE SET current = false, endDate = staged_updates.effectiveDate -- Set current to false and endDate to source's effective date.
WHEN NOT MATCHED THEN
INSERT(customerid, address, current, effectivedate, enddate)
VALUES(staged_updates.customerId, staged_updates.address, true, staged_updates.effectiveDate, null) -- Set current to true along with the new address and its effective date.
在這種情況下,除了 之外,我如何使用第二列customerId來充當 a mergeKey?
uj5u.com熱心網友回復:
只需將它們組合使用AND:
ON customers.customerId = staged_updates.customerId
AND customers.<second_column> = staged_updates.<second_column>
與在兩個表之間進行 JOIN 時相同 - 您需要提供連接條件
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/483630.html
