【问题标题】:MSSQL comparing 2 columns issueMSSQL比较2列问题
【发布时间】:2021-03-12 14:28:27
【问题描述】:

我有两张桌子: 客户信息,如 id、customerName、address 等。以及 RecipientOfGoods - customerId、地址。 两个表中的地址不同,客户表中的地址是注册地址,第二个表中的地址是交货地址。我需要在第一个表中添加收货地址,以便比较两种地址。

use dwh01
SELECT distinct
       cus.[BranchId]
      ,cus.[CustomerId]
      ,cus.[CustomerName]
      ,cus.[Street] as StreetByReg
      ,del.Street as StreetForDelivery
      ,cus.[Postalcode] as PKByReg
      ,del.Postalcode as PKForDelivery
      ,cus.[City] as CityByReg
      ,del.City as CityForDelivery
      ,cus.[PhoneNumber]
      ,cus.[EMail]
      ,cus.[IsActive]
      ,cus.[LastChangeDate]
  FROM [dwh01].[live].[DimCustomer] cus 
 join live.DimRecipientOfGoods del on del.RecipientOfGoodsid = cus.Customerid
 where cus.BranchId in('1080','1081') and ltrim(cus.CustomerId) = '99060' 

这就是我的查询,我确实得到了所需的结果,但我在第二行也得到了相同的信息,但在第二个表的地址列中,我从第一个表中得到了地址。问题是如何删除出现的无用的第二行以及为什么会这样?

【问题讨论】:

  • 您能否也向我们提供您用于获得此结果的数据。我的猜测是连接表中只有不止一行,但我需要查看这些数据才能确定
  • 您的客户似乎有多个交货行
  • 我想答案是:鉴于DimRecipientOfGoods 有重复的行,您希望它显示什么?它应该怎么做?聚合?每组前 1 名?

标签: sql sql-server comparison


【解决方案1】:

JOIN 排除相等的行以仅获取具有新地址的客户。

SELECT distinct
       cus.[BranchId]
      ,cus.[CustomerId]
      ,cus.[CustomerName]
      ,cus.[Street] as StreetByReg
      ,del.Street as StreetForDelivery
      ,cus.[Postalcode] as PKByReg
      ,del.Postalcode as PKForDelivery
      ,cus.[City] as CityByReg
      ,del.City as CityForDelivery
      ,cus.[PhoneNumber]
      ,cus.[EMail]
      ,cus.[IsActive]
      ,cus.[LastChangeDate]
  FROM [dwh01].[live].[DimCustomer] cus 
  JOIN live.DimRecipientOfGoods del ON del.RecipientOfGoodsid = cus.Customerid 
     AND (cus.[Street] <> del.[Street] OR cus.[Postalcode] <> del.[Postalcode] OR cus.[City] <> del.[City]) 
  WHERE cus.BranchId in('1080','1081') and ltrim(cus.CustomerId) = '99060' 

【讨论】:

    【解决方案2】:

    请使用左连接代替内部连接从 DimCustomer 获取所有客户信息,但仅从 DimRecipientOfGoods 获取其他地址信息。

    由于第二个表中有两个单独的地址信息,一个与第一个表完全匹配。您可以从第一个表中获得一个地址信息,并且 在第二个表中,您可以通过比较两个地址来忽略该地址。但我不确定这个条件是否对所有客户都一样。如果您想删除那些拥有 DimRecipientOfGoods 表中没有记录。

     use dwh01
        SELECT Distinct
               cus.[BranchId]
              ,cus.[CustomerId]
              ,cus.[CustomerName]
              ,cus.[Street] as StreetByReg
              ,del.Street as StreetForDelivery
              ,cus.[Postalcode] as PKByReg
              ,del.Postalcode as PKForDelivery
              ,cus.[City] as CityByReg
              ,del.City as CityForDelivery
              ,cus.[PhoneNumber]
              ,cus.[EMail]
              ,cus.[IsActive]
              ,cus.[LastChangeDate]
          FROM [dwh01].[live].[DimCustomer] cus 
         Left join live.DimRecipientOfGoods del on del.RecipientOfGoodsid = cus.Customerid
     AND (cus.Street <> del.Street OR cus.Postalcode <> del.Postalcode OR cus.City <> del.City) 
         where cus.BranchId in('1080','1081') and ltrim(cus.CustomerId) = '99060' 
    

    【讨论】:

    • distinct 不行,看他的图,有些列在重复行中有不同的值
    • 相同,结果相同。
    • @svetozarslavkov 您在 DimCustomer 表中有重复条目吗?请分享示例数据。
    • @KaziMohammadAliNur 不,我在 2 个表中的任何一个中都没有重复数据,我们有 2 个分支 1080 和 1081,在某些情况下,两个分支中都创建了 1 个客户,但 ID 不同。跨度>
    • 如果没有看到产生此结果的数据,任何人都无法分辨出重复行的来源。请向我们提供这些数据,帮助我们帮助您
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-04
    • 1970-01-01
    • 2022-06-26
    • 1970-01-01
    • 1970-01-01
    • 2014-05-18
    • 2018-10-03
    相关资源
    最近更新 更多