【问题标题】:SQL pattern matchingSQL 模式匹配
【发布时间】:2012-10-18 03:42:58
【问题描述】:

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

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

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

This is my first assignment in SQL 

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

My first assignment in SQL 

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

我做了一些研究,想要第二个意见,而且我从来没有要求过源代码。我已经使用 Levenshtein 距离算法查看了 Soundex()、Difference()、模糊字符串匹配。

【问题讨论】:

  • 提示:查看hamming distance 和类似的字符串相似度算法
  • 我做了一些研究,想要第二个意见,而且我从来没有要求过源代码。我已经使用 Levenshtein 距离算法查看了 Soundex()、Difference()、模糊字符串匹配。不过还是感谢您的提示

标签: sql oracle matching


【解决方案1】:

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

-- 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

结果:

         %
----------
     70.58

附录

正如@jonearles 正确指出的那样,使用edit_distance_similarity 包的edit_distance_similarity 函数要简单得多。

 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
   ;

结果:

         %
----------
        71

【讨论】:

  • 谢谢!我正在使用 Oracle 11G。我没想到会有任何代码,所以谢谢!
  • +1 您可以使用utl_match.edit_distance_similarity(col1, col2) 简化此操作。
猜你喜欢
  • 1970-01-01
  • 2018-05-29
  • 1970-01-01
  • 1970-01-01
  • 2011-12-03
  • 2020-03-07
  • 1970-01-01
  • 2011-11-02
  • 1970-01-01
相关资源
最近更新 更多