0

我正在努力录制缓慢变化的维度 (SCD)。我有一个日常 ETL 作业,根据更改注册记录版本。

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

相反,这就是我得到的

我知道造成这种情况的是 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';
4

0 回答 0