这是我解决这个问题的尝试,我希望其他人能找到更有效的方法:
select #t1.id1, #t1.adm1, #t1.dx1, #t2.id2, #t2.adm2, #t2.dx2, iif(#t1.dx1=#t2.dx2,1,0) as shared Into #t3 From #t1 cross join #t2
Select id1, adm1, dx1, id2, adm2, sum(shared) as In1In2, iif(sum(shared)=0,1,0) as In1Not2 into #t4 from #t3 group by id1, adm1, dx1, id2, adm2
Select id1, adm1, dx1, id2, adm2, sum(In1Not2) as nIn1Not2, into #t5 from #t4 group by id1, adm1, id2, adm2
Select id1, adm1, dx2, id2, adm2, iif(sum(shared)=0,1,0) as In2Not1 into #t6 from #t3 group by id1, adm1, dx2, id2, adm2
Select id1, adm1, id2, adm2, sum(In2Not1) as nIn2Not1 into #t7 from #t6 group by id1, adm1, id2, adm2
在下一步中,计算的值被合并到一个公用表中。此尝试的问题在于,在 100,000 条记录的 t1 和 400,000 条记录的 t2 上运行它需要 2 多个小时。