我正在创建一个用于“合并”和删除表中重复行的脚本。该表包含地址信息,并使用整数字段将有关电子邮件的信息存储为位标志(列名 lngValue)。例如,lngValue & 1 == 1 表示它的主地址。
有两次输入同一电子邮件的情况,但有时使用不同的 lngValues。为了解决这个问题,我需要从所有重复项中获取 lngValue 并将它们分配给一个幸存的记录并删除其余记录。
迄今为止,我最头疼的就是记录的“合并”。我想要做的是按位或重复记录的所有 lngValues 一起。这是我到目前为止所拥有的,它只找到所有 lngValues 的值按位或一起。
警告:前面的代码混乱
declare @duplicates table
(
lngInternetPK int,
lngContactFK int,
lngValue int
)
insert into @duplicates (lngInternetPK, lngContactFK, lngValue)
(
select tblminternet.lngInternetPK, tblminternet.lngContactFK, tblminternet.lngValue from tblminternet inner join
(select strAddress, lngcontactfk, count(*) as count from tblminternet where lngValue & 256 <> 256 group by strAddress, lngcontactfk) secondemail
On tblminternet.strAddress = secondemail.strAddress and
tblminternet.lngcontactfk = secondemail.lngcontactfk
where count > 1 and tblminternet.strAddress is not null and tblminternet.lngValue & 256 <> 256 --order by lngContactFK, strAddress
)
update @duplicates set lngValue = t.val
from
(select (sum(dupes.lngValue) & 65535) as val from
(select here.lngInternetPK, here.lngContactFK, here.lngValue from tblminternet here inner join
(select strAddress, lngcontactfk, count(*) as count from tblminternet where lngValue & 256 <> 256 group by strAddress, lngcontactfk) secondemail
On here.strAddress = secondemail.strAddress and
here.lngcontactfk = secondemail.lngcontactfk
where count > 1 and here.strAddress is not null and here.lngValue & 256 <> 256) dupes, tblminternet this
where this.lngContactFK = dupes.lngContactFK
) t
where lngInternetPK in (select lngInternetPK from @duplicates)
编辑:
这里要求的是一些示例数据:
表名:tblminternet
列名:
lngInternetPK
lngContactFK
lngValue
strAddress
示例第 1 行:
lngInternetPK:1
lngContactFK:1
lngValue:33
strAddress:“me@myaddress.com”
示例第 2 行:
lngInternetPK:2
lngContactFK:1
lngValue:40
strAddress:“me@myaddress.com”
如果这两个在这里合并是期望的结果:
lngInternetPK: 1
lngContactFK: 1
lngValue: 41
strAddress: "me@myaddress.com"
其他必要规则:
每个联系人可以有多个电子邮件,但每个电子邮件行必须是不同的(每个电子邮件只能显示为一行)。