通过找到的可能匹配项合并记录

Merging records by possible match found

我 运行 遇到了一个问题,我必须更正一些历史数据。它有大量 data.To 更正那些历史数据,我需要通过找到的可能匹配项将它们合并在一起。如果这与其他任务重复,请告诉我。

这是 table 结构:

CREATE TABLE Contacts
(
    Id INT PRIMARY KEY, 
    FirstName VARCHAR(50), 
    LastName VARCHAR(50), 
    Email VARCHAR(50), 
    Mobile VARCHAR(50),
    Notes VARCHAR(MAX),
)

合并逻辑如下:

 --When all 4 fields(firstName, lastName, Email, Mobile) are matching for more then one contact, merge them together
 --when one record has all 4 fields, another records has only 3 matching and 4th one as null, merge them,
 --when one record has all 4 fields, another records has only 2 matching and remaining two as null, merge them,
 --when one record has all 4 fields, another records has only 1 matching and remaining three as null, merge them,


 --when one record has 3 fields and 4th field is NULL, another record has exacly same matching records, merge them,
 --when one record has 3 fields and 4th field is NULL, another records has only 1 matching and remaining three as null, merge them,
 --when one record has 3 fields and 4th field is NULL, another records has only 2 matching and remaining two as null, merge them,
 --when one record has 3 fields and 4th field is NULL, another records has only 1 matching and remaining three as null, merge them,

 --when one record has 2 fields and 2 fields as NULL, another record has exacly same matching records, merge them,
 --when one record has 2 fields and 2 fields as NULL, another records has only 1 matching  field and remaining three as null, merge them,

 --when one record has 1 fields and 3 fields as NULL, another record has exacly same matching, merge them,

当我说将它们合并在一起时,这意味着将两个项目合并为一个并删除剩余的一个。我试图通过联系人列表上的光标来执行此操作,但这对我进行所有这些组合没有帮助。

我在这里找不到任何这样的 post 以及从哪里可以得到任何线索。任何有关编写查询以执行此操作的线索都会有所帮助。

CURSOR 有什么性能问题,考虑到很多情况,您可以使用 CURSOR 尝试下面的选项并检查您的要求是否完全填充-

DEMO HERE

DECLARE 
    @FirstName VARCHAR(MAX),
    @LastName VARCHAR(MAX),
    @Email VARCHAR(MAX),
    @Mobile  VARCHAR(MAX),

    @FirstName_prev VARCHAR(MAX),
    @LastName_prev VARCHAR(MAX),
    @Email_prev VARCHAR(MAX),
    @Mobile_prev  VARCHAR(MAX),

    @loop_start  INT = 0;

DECLARE @tmp TABLE(
    FirstName VARCHAR(MAX),
    LastName VARCHAR(MAX),
    Email VARCHAR(MAX),
    Mobile  VARCHAR(MAX)
);

DECLARE cursor_Contacts CURSOR
FOR SELECT FirstName,LastName,Email,Mobile
    FROM Contacts
    ORDER BY 
    ISNULL(FirstName,'ZZZZZZZZZZZZZZZ')
    ,ISNULL(LastName,'ZZZZZZZZZZZZZZZ')
    ,ISNULL(Email,'ZZZZZZZZZZZZZZZ')
    ,ISNULL(Mobile,'ZZZZZZZZZZZZZZZ');

OPEN cursor_Contacts;

FETCH NEXT FROM cursor_Contacts INTO 
     @FirstName,@LastName,@Email,@Mobile;

WHILE @@FETCH_STATUS = 0
    BEGIN

        IF @loop_start = 0

        BEGIN

            INSERT INTO @tmp(FirstName,LastName,Email,Mobile)
            VALUES (@FirstName,@LastName,@Email,@Mobile)

            SET @loop_start = 1

        END

        ELSE
        BEGIN

            IF
            (@FirstName_prev = @FirstName OR @FirstName IS NULL) AND
            (@LastName_prev = @LastName OR @LastName IS NULL) AND
            (@Email_prev = @Email OR @Email IS NULL) AND
            (@Mobile_prev = @Mobile OR @Mobile IS NULL)

            BEGIN
                SET @loop_start = 1
            END

            ELSE
            BEGIN
                SET @loop_start = 2

                INSERT INTO @tmp(FirstName,LastName,Email,Mobile)
                VALUES (@FirstName,@LastName,@Email,@Mobile)
            END
        END;


        SET @FirstName_prev = @FirstName
        SET @LastName_prev= @LastName
        SET @Email_prev = @Email
        SET @Mobile_prev= @Mobile;


        FETCH NEXT FROM cursor_Contacts INTO 
            @FirstName,@LastName,@Email,@Mobile;
    END;

CLOSE cursor_Contacts;

SELECT * FROM @tmp;

DEALLOCATE cursor_Contacts;