错误的城市,州,邮政编码审核

时间:2015-07-07 15:40:16

标签: sql-server-2008 tsql

我目前正在尝试审核系统中的无效信息。我想将companies_address与我的xro_zips表进行比较,该表包含City,State,Zipcode。 我可以返回一些在这两个表之间不匹配的邮政编码,但我还想返回错误的城市名称和州名缩写,其中邮政编码在两个表格之间匹配。

这是我到目前为止所拥有的......

SELECT DISTINCT 'workplace_base' as [db_id], c.external_id, c.company, ca.city, 
   ISNULL(ca.[state],'') as [state], ISNULL(ca.zip_code,'') as zip_code, d.division
FROM Workplace_base.dbo.companies_address ca
   INNER JOIN Workplace_base.dbo.companies c ON ca.company_id = c.company_id
   INNER JOIN Workplace_base.dbo.divisions d ON c.owner_division_id = d.division_id
   LEFT JOIN Workplace_base.dbo.xro_zips z ON ca.zip_code = z.ZipCode
      and ca.[state] = z.[State]
WHERE z.ZipCode IS NULL

任何帮助都会很棒。提前谢谢你。

1 个答案:

答案 0 :(得分:0)

这种事情应该给出所有差异:

SELECT * FROM (
SELECT DISTINCT 
    'workplace_base' as [db_id], 
    c.external_id, 
    c.company, 
    ca.city AS City1, 
    z.City AS City2, 
    ISNULL(ca.[state],'') as [State1], 
    ISNULL(z.[State],'') as [State2], 
    ca.zip_code as zip_code1,
    z.ZipCode as zip_code2,
    ISNULL(ca.zip_code,'Missing ZIP') as zip_code_check1, 
    ISNULL(z.ZipCode,'Extra ZIP') as zip_code_check2, 
    d.division,
    CASE WHEN ca.city <> z.city THEN 'City Mismatch' ELSE 'OK' END AS CityCheck,    
    CASE WHEN ca.[state] <> z.[state] THEN 'State Mismatch' ELSE 'OK' END AS StateCheck,    
FROM Workplace_base.dbo.companies_address ca
INNER JOIN Workplace_base.dbo.companies c ON ca.company_id = c.company_id
INNER JOIN Workplace_base.dbo.divisions d ON c.owner_division_id = d.division_id
FULL OUTER JOIN Workplace_base.dbo.xro_zips z ON ca.zip_code = z.ZipCode
) AS Comparison
WHERE zip_code1 IS NULL or zip_code2 IS NULL OR CityCheck <> 'OK' OR StateCheck <> 'OK'