如何解决动态实现Scd Type2
大家好,我想使用databricks和deltalake实现scd type 2。但是我希望代码是动态的。说有n个表,所以我不应该重写代码。
示例 这里的客户是增量表,更新是记录的更新表。现在,如果有不同的文件,则代码应该是动态的。
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.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。