我有一个与 SQL 相关的问题。
我想匹配两个字段的相似性并返回相似程度的百分比。
例如,如果我有一个名为 doc 的字段,其中包含以下内容
This is my first assignment in SQL
在另一个领域我有类似的东西
My first assignment in SQL
我想知道如何检查两者之间的相似性并返回多少百分比。
我做了一些研究并想要第二个意见,而且我从未要求过源代码。我研究过 Soundex()、Difference()、使用 Levenshtein 距离算法的模糊字符串匹配。
你没有说你用的是什么版本的Oracle。本示例基于11g版本。
您可以使用edit_distance
的函数utl_match http://docs.oracle.com/cd/E11882_01/appdev.112/e25788/u_match.htm#CBHBICDE包来确定需要更改多少个字符才能将一个字符串转换为另一个字符串。greatest http://docs.oracle.com/cd/E11882_01/server.112/e26088/functions069.htm#i77473函数返回传入参数列表中的最大值。这是一个例子:
-- 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
result:
%
----------
70.58
Addendum
正如@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
;
Result:
%
----------
71
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)