使用模糊逻辑基于匹配的列值更新另一个表中的列

时间:2016-06-28 11:30:23

标签: sql-server

如何根据两个表中另外两列的匹配百分比来更新另一个表中的列

例如假设我的表Company1具有列Address1和CompanyName1,另一个表Company2具有列Address2和CompanyName2。我想根据Address1和Address2更新Company2中Company1的CompanyName1 匹配百分比值。 请建议我一个有效的运行查询

1 个答案:

答案 0 :(得分:0)

根据您的评论:

;WITH Company2 as (
SELECT  'Hamby Chiropractic' as CompanyName2,
        '6716 Madison Ave # A1' as Address2
), Company1 as (
SELECT  NULL as CompanyName1,
        '6716 Madison' as Address1
)

SELECT *, LEN(c1.Address1)*100.00/LEN(c2.Address2)
FROM Company1 c1
LEFT JOIN Company2 c2
    ON c2.Address2 LIKE '%' + c1.Address1 +'%'
    OR c1.Address1 LIKE '%' + c2.Address2 +'%'

我会得到

CompanyName1    Address1        CompanyName2        Address2                (No column name)
NULL            6716 Madison    Hamby Chiropractic  6716 Madison Ave # A1   57.1428571428571

我根据字符串的长度计算了百分比。这对你来说足够了吗?