0

一部の SCD は必須ではなく、多数の重複を生成する SELECT EXCEPT ステートメントを使用して実装されているため、DW からいくつかの SCD を削除しています。孤立したレコードがないように、ファクト テーブルの参照をリダイレクトしたいと考えています。下のカーソルを使用してこれを達成することができました(と思います)。より滑らかな方法があったかどうか疑問に思いましたか?

DECLARE RemoveOldKeys CURSOR READ_ONLY
FOR
    SELECT  Teamid ,
            MAX(Teamkey)       -- latest surrogate key value generated by SCD code that is to be removed
    FROM    dbo.Team c1
    WHERE   EXISTS ( SELECT teamid ,          -- select entries from the team table in DW that have more than entry
                            COUNT(*)
                     FROM   dbo.team c2
                     WHERE  c1.teamid = c2.teamid
                     GROUP BY teamid
                     HAVING COUNT(*) > 1 )
    GROUP BY teamid
    ORDER BY c1.teamid


DECLARE @teamid UNIQUEIDENTIFIER ,
        @CurrentTeamkey INT 

OPEN RemoveOldKeys
FETCH NEXT FROM RemoveOldKeys INTO @teamid, @CurrentTeamKey
WHILE @@fetch_status = 0
    BEGIN

        UPDATE  investigation
        SET     investigation.TeamKey = @CurrentTeamKey
        FROM    dbo.Investigation i
                INNER JOIN dbo.Team t ON t.TeamKey = i.TeamKey
        WHERE   t.teamID = @teamID
                AND i.teamkey <> @CurrentTeamKey     -- no need to update if the key is already correct


        FETCH NEXT FROM RemoveOldKeys INTO @teamid, @CurrentTeamKey
    END

CLOSE RemoveOldKeys
DEALLOCATE RemoveOldKeys

GO
4

1 に答える 1

1
UPDATE investigation
   SET investigation.TeamKey = teamMax.maxTeamKey
  FROM dbo.Investigation 
  join team
    on teamMax.TeamKey = investigation.TeamKey 
  join (SELECT Teamid, MAX(Teamkey) as maxTeamKey
          FROM team
         group BY teamid
        having count(*) > 1 
       )  teamMax
    on teamMax.Teamid = team.teamid
   and investigation.TeamKey <> teamMax.maxTeamKey
于 2014-07-30T15:22:26.433 に答える