我遇到了必须纠正一些历史数据的问题。它具有大量数据。要更正这些历史数据,我需要通过可能的匹配将它们合并在一起。让我知道这是否与其他任务重复。
这里是表结构:
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,
[当我说将它们合并在一起时,这意味着将两项合并为一项,并删除其余项。我试图通过联系人列表上的光标来做这些事情,但这对所有这些组合都没有帮助。
我也无法从那里找到任何线索。任何编写查询来执行此操作的线索都将有所帮助。
[CURSOR遇到任何性能问题,考虑到很多情况,您可以使用CURSOR尝试以下选项并检查您的需求是否已满-
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;