2016-10-18 3 views
0

私は訂正しようとしている間にひどく同期していない2つのベンダーデータベースを持っています。単一の顧客に複数のid_numbersを割り当てることができ、これらのIDは両方のベンダーのデータベースに存在します。 1人の顧客のすべてのIDは、Vendor1データベースの1つの顧客レコードに正しく添付されています(つまり、同じcustomer_codeに属していることを意味します)。しかし、問題は、同じIDがVendor2データベース内の複数の顧客に分割されている可能性がありますが、これは間違っています。複数の顧客をVendor2データベースにまとめてマージする必要があります。別のベンダーデータベースに2つのレコードとして存在するレコードを1つ探します

2番目のベンダーのデータベースで2つ以上の顧客として表される顧客を特定しようとしています。これまでのところ、私は2つを一緒に結合しましたが、同じcustomer_codeの2つ以上の別個の顧客を見つける方法を見つけることはできません。MemberInternalKeys

は、ここで私がこれまで持っているものだ:以下の例では

select top 10 
    c.customer_code, 
    i.id_number, 
    cc.MemberInternalKey 
from Vendor1.dbo.customer_info as c 
join Vendor1.dbo.customer_ids as i 
    on c.customer_code = i.customer_code 
join Vendor2.dbo.Clubcard as cc 
    on (i.id_number collate Latin1_General_CI_AS_KS) = cc.ClubCardId 
where i.id_code = 'PS' 

、私はテーブル内の最後の2行を取り戻すことを期待します。最初の2行は、両方のレコードで同じMemberInternalKeyを持ち、同じcustomer_codeに属しているため、結果に含めないでください。両方のベンダーデータベース間に1-1の一致があるため、3行目も含めないでください。

customer_code | id_number | MemberInternalKey 
--------------|-----------|------------------ 
5549032  | 4000  | 4926877 
5549032  | 4001  | 4926877 
5031101  | 4007  | 2379218 
2831779  | 4029  | 1763760 
2831779  | 4062  | 4950922 

何か助けていただければ幸いです。

答えて

1

私が正しく理解していれば、あなたはこのロジックのためにウィンドウ関数を使用することができます。

select c.* 
from (select c.customer_code, i.id_number, cc.MemberInternalKey, 
      min(MemberInternalKey) over (partition by customer_code) as minmik, 
      max(MemberInternalKey) over (partition by customer_code) as maxmik 
     from Vendor1.dbo.customer_info c join 
      Vendor1.dbo.customer_ids i 
      on c.customer_code = i.customer_code join 
      Vendor2.dbo.Clubcard as cc 
      on (i.id_number collate Latin1_General_CI_AS_KS) = cc.ClubCardId 
     where i.id_code = 'PS' 
    ) c 
where minmik <> maxmik; 

これは、各customer_codeの最小値と最大MemberInternalKeyを計算します。外側のwhereは、これらが異なる行だけを返します。

1

別のオプションがある

Declare @YourTable table (customer_code int, id_number int, MemberInternalKey int) 
Insert Into @YourTable values 
(5549032,4000,4926877), 
(5549032,4001,4926877), 
(5031101,4007,2379218), 
(2831779,4029,1763760), 
(2831779,4062,4950922) 

Select A.* 
From @YourTable A 
Join (
     Select customer_code 
     From @YourTable 
     Group By customer_code 
     Having min(MemberInternalKey)<>max(MemberInternalKey) 
    ) B on A.customer_code=B.customer_code 

戻り

customer_code id_number MemberInternalKey 
2831779   4029  1763760 
2831779   4062  4950922 
関連する問題