SQL模式匹配

use*_*162 5 sql oracle matching

我有一个与SQL有关的问题.

我想匹配两个相似的字段,并返回一个百分比的相似程度.

例如,如果我有一个名为doc的字段,其中包含以下内容

This is my first assignment in SQL 
Run Code Online (Sandbox Code Playgroud)

在另一个领域,我有类似的东西

My first assignment in SQL 
Run Code Online (Sandbox Code Playgroud)

我想知道如何检查两者之间的相似性并返回多少百分比.

我做了一些研究,想要第二个意见加上我从未要求过源代码.我看过Soundex(),差异(),使用Levenshtein距离算法的模糊字符串匹配.

Nic*_*nov 5

您没有说明您正在使用的Oracle版本.此示例基于11g版本.您可以使用utl_match包的edit_distance功能来确定需要更改多少个字符才能将一个字符串转换为另一个字符串.最大函数返回传入参数列表中的最大值.这是一个例子:

-- sample of data 
with t1(col1, col2) as(
  select 'This is my first assignment in SQL', 'My first assignment in SQL ' from dual
)
-- the query
select trunc(((greatest(length(col1), length(col2)) -  
              (utl_match.edit_distance(col2, col1))) * 100) / 
             greatest(length(col1), length(col2)), 2) as "%"
  from t1
Run Code Online (Sandbox Code Playgroud)

结果:

         %
----------
     70.58
Run Code Online (Sandbox Code Playgroud)

附录

正如@jonearles正确指出的那样,使用包的edit_distance_similarity功能要简单得多utl_match.

 with t1(col1, col2) as(
     select 'This is my first assignment in SQL', 'My first assignment in SQL ' from dual
  )
  select utl_match.edit_distance_similarity(col1, col2) as "%"
    from t1
   ;
Run Code Online (Sandbox Code Playgroud)

结果:

         %
----------
        71
Run Code Online (Sandbox Code Playgroud)

  • +1你可以用`utl_match.edit_distance_similarity(col1,col2)`简化这个. (2认同)