假设我有以下客户表:
<表类>
行
邮件
手机
组
tbody><<tr>1 Dan@gmail.com + 135698887445 1 2Danny@gmail.com + 145698834549 2 表类>
首先,定义如果多行实际上属于同一组,则以最小的组为准。然后,我们只需要循环更新,直到没有发现组减少:
declare @rc bigint=1 -- to go through first iteration check
declare @counter_safety_max=100
declare @counter=1
while(@rc>0 and @counter<@counter_safety_max)
begin
set @counter+=1
update current
set group=lowest.group
from
customer current
cross apply
(
select top 1 group
from customer cl
where (cl.phone=current.phone or cl.email=current.email) and cl.group<current.group
order by group asc
) as lowest
set @rc=@@rowcount
end