0

残念ながら、多数の重複レコードがあるディメンション Users テーブルがあります。スクリーンショットを参照してください。 重複レコード。

何千ものユーザーと、重複を参照する 5 つのテーブルがあります。「不良」のレコードを削除したいUserIDUserId5 つの依存関係を調べて、悪いs を「良い」(赤丸で囲った部分) に更新したいと考えています。

これに対する良いアプローチは何でしょうか?

上記のスクリーンショットを取得するために私がしたことは次のとおりです...

SELECT UserID
    ,userIds.FirstName
    ,userIds.LastName
    ,dupTable.Email
    ,dupTable.Username
    ,dupTable.DupCount
FROM dbo.DimUsers AS userIds
LEFT OUTER JOIN
    (SELECT FirstName
        ,LastName
        ,Email
        ,UserName
        ,DupCount
    FROM
        (SELECT FirstName
            ,LastName
            ,UserName
            ,Email
            ,COUNT(*) AS DupCount -- we're finding duplications by matches on FirstName,
                                    -- last name, UserName AND Email.  All four fields must match
                                    -- to find a dupe.  More confidence from this.
        FROM dbo.DimUsers
        GROUP BY FirstName
            ,LastName
            ,UserName
            ,Email
        HAVING COUNT(*) > 1) AS userTable -- any count more than 1 is a dupe
        WHERE LastName NOT LIKE 'NULL' -- exclude entries with literally NULL names
            AND FirstName NOT LIKE 'NULL'
        )AS dupTable
ON dupTable.FirstName = userIds.FirstName -- to get the userIds of dupes, we LEFT JOIN the original table
    AND dupTable.LastName = userIds.LastName -- on four fields to increase our confidence
    AND dupTable.Email = userIds.Email
    AND dupTable.Username = userIds.Username
WHERE DupCount IS NOT NULL -- ignore NULL dupcounts, these are not dupes
4

1 に答える 1

0

1 つの依存関係テーブル用に作成されたこのコードは機能するはずですが、同じロジックを使用して他の 4 つのテーブルを更新できます。

update t
set UserID = MinUserID.UserID
from
  DimUsersChild1 t
  inner join DimUsers on DimUsers.UserID = t.UserID
  inner join (
              select min(UserID) UserID, FirstName, LastName, UserName, Email
              from DimUsers
              group by
                FirstName, LastName, UserName, Email
              ) MinUserID on 
                          MinUserID.FirstName = DimUsers.FirstName and
                          MinUserID.LastName = DimUsers.LastName and
                          MinUserID.UserName = DimUsers.UserName and
                          MinUserID.Email = DimUsers.Email

select * from DimUsersChild1;

delete t1
from
  DimUsers t
  inner join DimUsers t1 on t1.FirstName = t.FirstName and
                            t1.LastName = t.LastName and
                            t1.UserName = t.UserName and
                            t1.Email = t.Email
where
t.UserID < t1.UserID


select * from DimUsers;

これが実際のデモです

于 2013-06-14T03:51:19.277 に答える