【发布时间】:2015-04-02 16:23:41
【问题描述】:
我正在创建一个用于“合并”和删除表中重复行的脚本。该表包含地址信息,并使用整数字段将有关电子邮件的信息存储为位标志(列名 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
lng值
字符串地址
示例第 1 行:
lngInternetPK:1
lngContactFK: 1
lngValue: 33
strAddress: "me@myaddress.com"
示例第 2 行:
lngInternetPK:2
lngContactFK: 1
lng值:40
strAddress: "me@myaddress.com"
如果这两个在这里合并是期望的结果:
lngInternetPK:1
lngContactFK: 1
lng值:41
strAddress: "me@myaddress.com"
其他必要的规则:
每个联系人可以有多个电子邮件,但每个电子邮件行必须是不同的(每封电子邮件只能显示为一行)。
【问题讨论】:
-
您可以分享一些示例数据吗?看看现在的数据是什么样的以及期望的结果可能有助于产生一些答案。
-
感谢您发布数据。它有帮助。