【发布时间】:2020-07-18 03:53:18
【问题描述】:
我遇到了一个问题,我必须更正一些历史数据。它有大量的数据。为了更正这些历史数据,我需要通过找到的可能匹配将它们合并在一起。让我知道这是否与其他任务重复。
这是表结构:
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,
当我说将它们合并在一起时,这意味着将两个项目合并为一个并删除剩余的一个。我试图通过联系人列表上的光标来做这些事情,但这并没有帮助我完成所有这些组合。
我在这里也找不到任何这样的帖子,我可以从那里获得任何线索。编写查询以执行此操作的任何线索都会有所帮助。
【问题讨论】:
-
你可以用一个很长而且很复杂的
where声明来做到这一点 -
样本数据和预期结果将有助于说明您的规则。
标签: sql sql-server sql-server-2008 sql-server-2012 ssms