首页 > 解决方案 > APEX Oracle 比较两个字段 (varchar2)

问题描述

我想比较两个 varchar2 字段并根据百分比相似性来获得这个百分比作为我的函数的结果,以及这个表中这个记录的 ID。

我有表 ( SYMPTOMS),我还有Symptom_Descr这个表的字段 (VARCHAR2) 和变量v_symptom(varchar2),我想将此变量与此字段进行比较。

例如,这是我的表:

在此处输入图像描述

我要比较的变量是: 'flashes the room lights 5 times'

结果我想要=

ID
1 0%
2 0%
3 90%

另一个例子,如果变量是'washes her hands 7 times'

ID
1 80%
2 0%
3 10%

上述百分比并不准确。

如果以上都做不到,那我该怎么做才能找到相似之处呢?

标签: sqloracle11goracle-apexoracle-apex-5.1

解决方案


您可以使用该UTL_MATCH软件包:

SELECT id,
       UTL_MATCH.EDIT_DISTANCE_SIMILARITY(
         symptom_descr,
         'flashes the room lights 5 times'
       ) AS ed_similarity,
       UTL_MATCH.JARO_WINKLER_SIMILARITY(
         symptom_descr,
         'flashes the room lights 5 times'
       ) AS jw_similarity
FROM  symptoms;

其中,对于样本数据:

CREATE TABLE symptoms (id, symptom_descr) AS
SELECT 1, 'washes his hands constantly' FROM DUAL UNION ALL
SELECT 2, 'checks several times if he turned off the water heater' FROM DUAL UNION ALL
SELECT 3, 'flashes the room lights too many times' FROM DUAL;

输出:

ID ED_SIMILARITY JW_SIMILARITY
1 30 62
2 25 62
3 79 93

db<>在这里摆弄


推荐阅读