2016-12-22 1 views
2

배경 - 고객 데이터 집합이 있고 문자열 일치 알고리즘을 사용하여 모든 레코드의 유사성을 비교합니다. 그런 다음 직접 또는 연관을 통해 서로 관련된 결과를 그룹화하고 각 그룹에 고유 한 ID를 적용해야합니다.문자열 일치 후 SQL Server 레코드 연결

문제 - 난 예를 함께 기록을 연결하고 각 그룹

에 대한 고유 ID를 적용하는 방법을 생각할 수 없다 데이터는 현재 발견 된 일치하는 다음과 같습니다

(MatchScore는 여기의 문제와 관련이 없지만 데이터가 어디서 왔는지 보여주기 위해 사용됩니다.)

+-------------+-------------+------------+ 
| CustomerID1 | CustomerID2 | MatchScore | 
+-------------+-------------+------------+ 
|  2021000 |  2707799 | 0.075  | 
|  2021000 |  3856308 | 0.082  | 
|  774062 |  774063 | 0.041  | 
|  998328 |  2278386 | 0.063  | 
|  998328 |  998329 | 0.058  | 
|  998329 |  2278386 | 0.030  | 
+-------------+-------------+------------+ 

하단 3 개 레코드가 모든 그러므로 내가 그들 연관된 동일한 ID를 갖고 싶어, 연결되어 있습니다.

visual image of these records all being related

내가 예를 들어 테이블

select '998328' as CustomerID1,'998329' as CustomerID2,'0.058' as MatchScore 
into #tmp 
union 
select '998328' as CustomerID1,'2278386' as CustomerID2,'0.063' as MatchScore 
union 
select '998329' as CustomerID1,'2278386' as CustomerID2,'0.030' as MatchScore 
union 
select '2021000' as CustomerID1,'2707799' as CustomerID2,'0.075' as MatchScore 
union 
select '2021000' as CustomerID1,'3856308' as CustomerID2,'0.082' as MatchScore 
union 
select '774062' as CustomerID1,'774063' as CustomerID2,'0.041' as MatchScore 

select * from #tmp 

를 생성하는 데이터가

+----+-------------+-------------+------------+ 
| ID | CustomerID1 | CustomerID2 | MatchScore | 
+----+-------------+-------------+------------+ 
| 1 |  998328 |  2278386 | 0.063  | 
| 1 |  998328 |  998329 | 0.058  | 
| 1 |  998329 |  2278386 | 0.030  | 
| 2 |  2021000 |  2707799 | 0.075  | 
| 2 |  2021000 |  3856308 | 0.082  | 
| 3 |  774062 |  774063 | 0.041  | 
+----+-------------+-------------+------------+ 

또는 유사

+----+------------+ 
| ID | CustomerID | 
+----+------------+ 
| 1 | 2278386 | 
| 1 |  998328 | 
| 1 |  998329 | 
| 2 | 2021000 | 
| 2 | 2707799 | 
| 2 | 3856308 | 
| 3 |  774062 | 
| 3 |  774063 | 
+----+------------+ 

코드를 같이 할 것입니다 내가 레코드를 서로 연결하는 방법을 생각할 수 없다고 말하면서 나는 모든 종류의 조인을 시도했지만 유레카 순간은 결코 오지 않는다. 제발 도와 줄 수 있어요.

감사

+3

하단의 3 개 레코드가 의미하는 것은 무엇입니까? 'CustomerID1'이 여러 개의 'CustomerId2' 값으로 나열되어 있기 때문에 연결되어 있습니까? 그리고 왜 'CustomerID1' 998328과 998329는 같은'ID' 값으로 끝나야합니까? – Taryn

+0

은 3 개의 개별 레코드가 고객 998328과 2278386이 일치하므로 998328과 998329가 일치하므로 998329와 2278386이 일치합니다. 따라서 모두 3 명이 서로 일치하는 것으로 표시되었으므로 동일한 ID를 얻으십시오. – DataPro

답변

1

나는 이것이 당신이 기대하는 결과입니다 확실하지 않다,

with tmp as(
select '998328' as CustomerID1,'998329' as CustomerID2,'0.058' as MatchScore 
union 
select '998328' as CustomerID1,'2278386' as CustomerID2,'0.063' as MatchScore 
union 
select '998329' as CustomerID1,'2278386' as CustomerID2,'0.030' as MatchScore 
union 
select '2021000' as CustomerID1,'2707799' as CustomerID2,'0.075' as MatchScore 
union 
select '2021000' as CustomerID1,'3856308' as CustomerID2,'0.082' as MatchScore 
union 
select '774062' as CustomerID1,'774063' as CustomerID2,'0.041' as MatchScore 
union 
select '774063' as CustomerID1,'774062' as CustomerID2,'0.041' as MatchScore 
union 
select '774063' as CustomerID1,'774063' as CustomerID2,'0.041' as MatchScore) 


select DENSE_RANK() OVER(ORDER BY rank_value) id, t1.CustomerID1, t1.CustomerID2 
from(
    select 
     t1.*, 
     case 
      when t2.CustomerID1 IS NOT NULL 
       THEN t2.CustomerID1 
      ELSE t3.CustomerID1 
     end rank_value 

    from tmp t1 
    left join tmp t2 
    on (t1.CustomerID1 = t2.CustomerID2 
      and t1.CustomerID2!=t2.CustomerID1 
      and (t1.CustomerID1 != t1.CustomerID2 and t2.CustomerID1 != t2.CustomerID2)) 
     or (t1.CustomerID1 = t2.CustomerID1 
      and t1.CustomerID2 != t2.CustomerID2 
      and (t1.CustomerID1 != t1.CustomerID2)) 
    left join tmp t3 
     on t1.CustomerID1 = t3.CustomerID2 
      and t1.CustomerID2=t3.CustomerID1 
)t1 

내가

enter image description here

참고 아래의 결과는 무엇입니까 : DENSE_RANK() 기능을 사용할 수를 버전 2012부터

+0

좋은 접근 방식이지만 약간의 버그가있는 것 같습니다. CustomerID1, '774062'를 CustomerID2, 'MatchScore를 0.041'(또는 ID1과 ID2를 774063)로 선택하여 'tmp에 다른 레코드를 추가하면' ID가 엉망이되었습니다 ... – Tyron78

+0

Tyron78의 말은 사실입니다.이 접근법은이 예제에서 작동하지만 데이터가 약간 변경되면 잘못된 결과가 나타납니다. 나는 멋진 세트 기반의 접근 방법이 있다는 것을 확신하지 못한다. 그러나 하나를 찾으면 여기에 다시 게시 할 것이다. – DataPro

+0

@ Tyron78 ​​그것은 정말로 좋은 캐치 다. 이에 따라 내 대답을 수정했습니다. – Viki888