Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 在另一个供应商数据库中查找作为两条记录存在的一条记录_Sql Server_Database - Fatal编程技术网

Sql server 在另一个供应商数据库中查找作为两条记录存在的一条记录

Sql server 在另一个供应商数据库中查找作为两条记录存在的一条记录,sql-server,database,Sql Server,Database,我有两个供应商数据库,这些年来我一直在努力纠正它们,但它们严重不同步。单个客户可以有多个id_编号,并且这些id存在于两个供应商数据库中。单个客户的所有ID都正确地附加到Vendor1数据库中的一个客户记录,这意味着它们属于相同的客户代码。然而,问题是,这些相同的ID可能会被Vendor2数据库中的多个客户分割,这是不正确的。我需要将这些多个客户合并到Vendor2数据库中 我试图确定哪些客户在第二个供应商数据库中表示为两个或多个客户。到目前为止,我已经将这两者结合在一起,但我不知道如何只找到具

我有两个供应商数据库,这些年来我一直在努力纠正它们,但它们严重不同步。单个客户可以有多个id_编号,并且这些id存在于两个供应商数据库中。单个客户的所有ID都正确地附加到Vendor1数据库中的一个客户记录,这意味着它们属于相同的客户代码。然而,问题是,这些相同的ID可能会被Vendor2数据库中的多个客户分割,这是不正确的。我需要将这些多个客户合并到Vendor2数据库中

我试图确定哪些客户在第二个供应商数据库中表示为两个或多个客户。到目前为止,我已经将这两者结合在一起,但我不知道如何只找到具有两个或多个不同MemberInternalKey的客户,这些密钥用于相同的customer_代码

以下是我目前掌握的情况:

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'
在下面的示例中,我希望只返回表中的最后两行。前两行不应包含在结果中,因为它们对于两个记录具有相同的MemberInternalKey,并且属于相同的customer_代码。第三行也不应包括在内,因为两个供应商数据库之间存在1-1匹配

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

非常感谢您的帮助。

如果我理解正确,您可以将窗口功能用于此逻辑:

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;

这将计算每个客户代码的最小和最大MemberInternalKey。然后,外部where仅返回不同的行。

如果我理解正确,您可以使用窗口函数实现此逻辑:

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;
这将计算每个客户代码的最小和最大MemberInternalKey。外部where只返回不同的行。

另一个选项是

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
另一个选择是

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