好的,这里有一些简化的表格来帮助解释我的情况
事实清单创建:
ListingCreatedSk
CreatedDateSk
ListingSk
StateSk
昏暗列表
ListingSk
ListingBk
StateCode
ListingPrice (SCD2)
ListingStatus (SCD2)
RowEffectiveDate
RowExpirationDate
RowCurrentIndicator
我遇到的问题是,当我将维度中的更新合并到我的基本事务事实表时,由于维度中的 SCD2 更改添加了新行,我最终在我的事实中出现重复条目(相同的 ListingBK)。处理这些情况的最佳方法是什么,我们的关键约束是我们希望事实中的每一行都指向维度表中的原始 Sk。
当前程序:
MERGE INTO dbo.FactListingCreated AS target
USING
(
SELECT dlm.CreatedDateSk,
dl.ListingSk,
CASE
WHEN db.BrokerageSk IS NULL THEN -1
ELSE db.BrokerageSk
END as BrokerageSk,
ds.StateSk
FROM stage.DimListingMerge as dlm
LEFT JOIN dbo.DimDate as dd
ON dd.DateSk = dlm.CreatedDateSk
LEFT JOIN dbo.DimListing as dl
ON dl.ListingBk = dlm.ListingBk
AND dl.RowCurrentIndicator = 1
LEFT JOIN dbo.DimBrokerage as db
ON db.BrokerageBk = dlm.BrokerageBk
LEFT JOIN dbo.DimState as ds
ON ds.StateCode = dlm.StateCode
) source
ON (target.ListingSk = source.ListingSk)
THEN UPDATE SET
target.CreatedDateSk = source.CreatedDateSk,
target.BrokerageSk = source.BrokerageSk,
target.StateSk = source.StateSk
WHEN NOT MATCHED THEN
INSERT VALUES
(
source.CreatedDateSk,
source.ListingSk,
source.StateSk
);
因此,我认为此过程适用于更新(仅提取前一天的数据),但是,最好的方法是进行单独的初始运行(从暗淡中提取所有数据),其中提取每条记录的初始行? 还是我错过了一些非常明显的东西,可以通过单个存储过程实现这一点?