1
我正在嘗試通過T-SQL實現緩慢更改維度類型2,但我無法弄清楚如何將請求發揮作用。SQL Server - 如何使用合併語句緩慢更改維度以上兩個條件?
表列:cpf
,nome
,telefone_update
,endereco_insert
基本上邏輯是:如果MATCH
不發生使用cpf
,則記錄必須被插入;如果MATCH
發生但只有telefone_update
字段已更改,則不需要另一條記錄,我只想更新和覆蓋值;如果MATCH
發生但只有endereco_insert
字段已更改我想添加一條新記錄並更新開始日期和結束日期。
我至今是:
insert into #dm_lucas_tst (
[cpf],
[nome],
[telefone_update],
[endereco_insert],
[dt_scd_start],
[dt_scd_end],
[nu_scd_version]
)
select [cpf],
[nome],
[telefone_update],
[endereco_insert],
cast(dateadd(month, datediff(month, 0, getdate()), 0) as date) as [dt_scd_start],
'2199-12-31' AS [dt_scd_end],
1 AS [nu_scd_version]
from (
merge edw.dim.dm_lucas_tst as Target
using edw.dim.stg_lucas_tst as Source
on Target.cpf = Source.cpf
when not matched by target
then
insert (
[cpf],
[nome],
[telefone_update],
[endereco_insert],
[dt_scd_start],
[dt_scd_end],
[nu_scd_version]
)
values (
Source.[cpf],
Source.[nome],
Source.[telefone_update],
Source.[endereco_insert],
cast(dateadd(month, datediff(month, 0, getdate()), 0) as date),
'2199-12-31',
1
)
when matched
and Source.telefone_update <> Target.telefone_update
and Target.dt_scd_end = '2199-12-31'
then
update set telefone_update = Source.telefone_update
output $ACTION ActionOut,
Source.[cpf],
Source.[nome],
Source.[telefone_update],
Source.[endereco_insert]
) AS MergeOut
where MergeOut.ActionOut = 'UPDATE';
但我不認爲把另一WHEN MATCH AND ...
將會使這項工作。
有什麼建議嗎?
在此先感謝!
嘿@盧卡斯,你解決了這個問題嗎? –
@KamilNowinski不是真的。我決定繼續前進,並插入一個新的行。 –
hi @Lucas,下面介紹的解決方案有幫助嗎? –