SCD类型2结束日期问题

时间:2018-02-14 12:13:01

标签: sql sql-server database etl scd

我正在努力录制缓慢变化的尺寸(SCD)。我有一个每日ETL工作,根据变化注册记录版本。

问题在于我的结束日期大于当前版本的开始日期。我应该看到的是,非当前记录的结束日期至少是当前记录开始日期的前一天。

Instead, this is what I get

我知道造成这种情况的原因是ETL工作。出于某种原因,它将非当前记录EndDate更新为今天的日期。一旦记录是非当前的,它就不应该再次更改EndDate。

有人可以指出我在代码中做错了吗?

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER Procedure [dbo].[UpdateBudget_Hist]
AS
  BEGIN
    Merge Into [dbo].[Budget_Hist] as TargetTable
            Using [dbo].[Budget] as SourceTable
              On TargetTable.ProjectKey = SourceTable.ProjectKey
              AND TargetTable.Titulo = SourceTable.Titulo
              AND TargetTable.DataIns = SourceTable.DataIns
                When Not Matched 
                    Then
                        INSERT 
                        Values ( SourceTable.[ProjectKey]
                                    , SourceTable.[Titulo]
                                    , SourceTable.[ActualCost]
                                    , SourceTable.[BaselineCost]
                                    , SourceTable.[UserIns]
                                    , SourceTable.[DataIns]
                                    , SourceTable.[UserUpd]
                                    , SourceTable.[DataUpd]
                                    , SourceTable.[Designation]
                                    , SourceTable.[Committed]
                                    , GetDate()  -- ChangeStartDate
                                    , Null  -- ChangeEndDate
                                    , 'y' )  -- IsCurrent
                When Matched -- When the matched fields of the row currently being looked at match 
                         -- but the rest does not match...
                         AND ( SourceTable.[ActualCost] <> TargetTable.[ActualCost] 
                         OR SourceTable.[BaselineCost] <> TargetTable.[BaselineCost] 
                         OR SourceTable.[Designation] <> TargetTable.[Designation] 
                         OR SourceTable.[Committed] <> TargetTable.[Committed] ) 
                Then 
                        -- Merge will not allow an insert statement to be used under the MATCHED clause
                        -- So, all we can do at this point is to change the IsCurrent status
                        UPDATE -- change the data in the Target table
                        SET TargetTable.IsCurrent = 'n'  -- IsCurrent
                When Not Matched By Source 
                    Then -- The matched fields are in the Target table, but not the source table
                        -- First, Change the current Matched row
                        UPDATE -- change the data in the Target table (DimCustomers)
                        SET TargetTable.ChangeEndDate = GetDate()  -- ADDED THIS for ChangeEndDate!
                             , TargetTable.IsCurrent = 'n';

    -- Add any Rows that were updated to the table Variable after we 
    -- synchronize any Inserts or Deletes.
    Insert into [dbo].[Budget_Hist]
        (  [ProjectKey]
          ,[Titulo]
          ,[ActualCost]
          ,[BaselineCost]
          ,[UserIns]
          ,[DataIns]
          ,[UserUpd]
          ,[DataUpd]
          ,[Designation]
          ,[Committed]
          ,[ChangeStartDate]
          ,[ChangeEndDate]
          ,[IsCurrent])
        Select [ProjectKey] ,[Titulo],[ActualCost],[BaselineCost],[UserIns],[DataIns],[UserUpd],[DataUpd],[Designation],[Committed], GetDate(), Null, 'y' from [dbo].[Budget]
        Except 
        Select [ProjectKey] ,[Titulo],[ActualCost],[BaselineCost],[UserIns],[DataIns],[UserUpd],[DataUpd],[Designation],[Committed], GetDate(), Null, 'y' from [dbo].[Budget_Hist]
  END

这是我认为问题所在,但我不知道如何解决它:

                    SET TargetTable.ChangeEndDate = GetDate()  -- ADDED THIS for ChangeEndDate!
                         , TargetTable.IsCurrent = 'n';

0 个答案:

没有答案