比较2个表中带有部分和不匹配列的地址

时间:2019-01-31 03:14:11

标签: sql sql-server common-table-expression temp-tables

我正在尝试进行比较,以查看相同的客户是否在不同销售数据库的2个表中。在大多数情况下,如果正确输入或不为空,则两个表中都有一个匹配的帐号。

如果没有匹配的帐号,我需要查看相似的地址。

现在我正在使用CTE,但是当没有帐号匹配时,我没有得到正确的结果来按地址相似性进行匹配。

使用临时表会更好,如果可以,怎么办?

这是我的代码:

-第一个cte提取所有非空帐户并重新格式化帐户号以匹配第二个表中帐户的格式

WITH Table1_Reformat_AccountNum_CTE
AS
(
  Select 
          t1.account_number
         ,t1.account_name
         ,t1.address_1 
         ,t1.address_2
         ,t1.city
         ,t1.state
         ,t1.zip
         ,CASE
            WHEN t1.account_number LIKE '%xyz-%' THEN LTRIM(RTRIM(RIGHT(t1.account_number, LEN(t1.account_number)-9)))
            WHEN t1.account_number LIKE 'qrs:% -lmo' THEN REPLACE(LTRIM(RTRIM(RIGHT(t1.account_number, LEN(t1.account_number)-5))), '-lmo', '')
            ELSE REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE( REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(RIGHT(t1.account_number, LEN(t1.account_number)-5), '-', ''), ' ', ''), '.', ''), ',', ''), 'out', ''), '#', ''), '0ut', ''), '''', ''), '_', ''), '*', ''),'<',''),'>',''),'^','')
            END AS account_num_formatted
  From [database_1].[dbo].[sales_table_1] t1
  WHERE t1.account_number IS NOT NULL


)
-- 2nd cte matched reformatted account numbers to account numbers in table 2
,Table2_AccountNum_Matches_CTE
AS
(
SELECT 
          t1.account_num_formatted 
         ,t2.account_number
         ,t2.account_name
         ,t2.address_1 
         ,t2.address_2
         ,t2.city
         ,t2.state
         ,t2.zip

FROM Table1_Reformat_AccountNum_CTE ref
INNER JOIN [database_2].[dbo].[sales_table_2] t2
ON t2.account_number = t1.account_num_formatted 
)
-- 3rd cte finds all left over accounts in table 1 that have no match in table 2

,Table1_Non_Matches_Cte
AS
(
SELECT 
          t1.account_num_formatted
         ,t1.account_name
         ,t1.address_1 
         ,t1.address_2
         ,t1.city
         ,t1.state
         ,t1.zip 
    FROM Table1_Reformat_AccountNum_CTE ref
    WHERE t1.account_num_formatted NOT IN (SELECT acm.account_num_formatted FROM Table2_AccountNum_Matches_CTE acm)
)
-- 3rd cte finds all left over accounts in table 2 that have no match in table 1

,Table2_Non_Matches_Cte
AS
(
SELECT  
          t2.account_number
         ,t2.account_name
         ,t2.address_1 
         ,t2.address_2
         ,t2.city
         ,t2.state
         ,t2.zip
    FROM [database_2].[dbo].[sales_table_2] t2
    WHERE t2.account_number NOT IN (SELECT acm.account_num_formatted FROM Table2_AccountNum_Matches_CTE acm)
)

-- attemtps to find all accounts in table 2 that have no match in account number in table 1 but match on address 

SELECT 
          t1.account_num_formatted
         ,t1.account_name
         ,t1.address_1 
         ,t1.address_2
         ,t1.city
         ,t1.state
         ,t1.zip 
         ,t2.account_number
         ,t2.account_name
         ,t2.address_1 
         ,t2.address_2
         ,t2.city
         ,t2.state
         ,t2.zip
    FROM Table1_Reformat_AccountNum_CTE ref
    FULL OUTER JOIN [database_2].[dbo].[sales_table_2] t2
    ON t1.account_num_formatted = t2.account_number
    WHERE t2.account_number IN (SELECT  non.account_number FROM Table2_Non_Matches_Cte non )
    AND t1.address_1  LIKE '%'+ t2.address_1 +'%'
    AND t1.city  LIKE '%'+ t2.city+'%'
    AND t2.state LIKE '%'+ t2.state+'%'
    AND t2.zip  = '%'+t2.zip+'%'

样本数据:

表1

——————
Acc | Name | Add 1| Add 2 | City | State | Zip
————————————————————-
1       store1   113 st lane    mulberry tx   12344
Null   store2   114 st lane    mulberry tx   34254
6       store3   113 st lane    mulberry tx   98764
7       store4   115 st lane    mulberry tx   74631
8.1    store5   116 st lane    mulberry tx   12347

表2

——————
Acc | Name | Add 1| Add 2 | City | State | Zip
————————————————————-
1       store1   113 st lane   mulberry tx   12344
Null   store2   114 st lane   mulberry tx   34254
9       store8   213 ha ave   juniper    ct   46738
5      store9   217 wa ave  juniper    ct  98376
8     store5   116 st lane    mulberry    tx   12347

预期结果:

Acc | Name | Add 1| Add 2 | City | State | Zip
————————————————————-
1        store1   113 st lane    mulberry tx   12344
Null   store2   114 st lane    mulberry tx   34254
8       store5   116 st lane   mulberry tx   12347

0 个答案:

没有答案
相关问题