如何在Oracle中使用UTL_匹配显示不同表中两列之间的差异?
我想比较Oracle DB中的两列,如果字符串不同,我想显示其他列中的差异。我知道我错过了什么。 例如:如何在Oracle中使用UTL_匹配显示不同表中两列之间的差异?,oracle,string-comparison,case-when,utl-match,Oracle,String Comparison,Case When,Utl Match,我想比较Oracle DB中的两列,如果字符串不同,我想显示其他列中的差异。我知道我错过了什么。 例如: SELECT A.CD_KEY01, A.TEXT_01, B.TEXT_02, UTL_MATCH.edit_distance_similarity(A.TEXT_01, B.TEXT_02) AS distance_similarity FROM TB_TABLE_01 A JOIN TB_TABLE_02 B ON A.CD_KEY01 =
SELECT A.CD_KEY01,
A.TEXT_01,
B.TEXT_02,
UTL_MATCH.edit_distance_similarity(A.TEXT_01, B.TEXT_02) AS distance_similarity
FROM TB_TABLE_01 A
JOIN TB_TABLE_02 B
ON A.CD_KEY01 = B.CD_KEY02
我得到的示例输出:
CD_KEY01 | TEXT_01 | TEXT_02 | DISTANCE_SIMILARITY
111 | Superman is good | Superman is good | 100
222 | Superman is bad | Superman is bad | 100
333 | Superman is handsome | Hulk is ugly | 33
444 | Superman is awful | Batman is awful | 90
我需要的输出示例:
CD_KEY01 | TEXT_01 | TEXT_02 | DISTANCE_SIMILARITY | DIFF_01 | DIFF_02
111 | Superman is good | Superman is good | 100 | NULL | NULL
222 | Superman is bad | Superman is bad | 100 | NULL | NULL
333 | Superman is handsome | Hulk is ugly | 33 | Hulk | ugly
444 | Superman is awful | Batman is awful | 90 | Batman | NULL
我怀疑有没有简单的方法。一种方法是以某种递归方式将字符串拆分为单词,逐字比较,并以透视结果为准:
with
a(key, text_a, word, rn) as (
select cd_key01, text_01, regexp_substr(text_01, '(\w+)', 1, 1), 1
from table_01
union all
select key, text_a, regexp_substr(text_a, '(\w+)', 1, rn + 1), rn + 1
from a
where regexp_substr(text_a, '(\w+)', 1, rn + 1) is not null),
b(key, text_b, word, rn) as (
select cd_key02, text_02, regexp_substr(text_02, '(\w+)', 1, 1), 1
from table_02
union all
select key, text_b, regexp_substr(text_b, '(\w+)', 1, rn + 1), rn + 1
from b where regexp_substr(text_b, '(\w+)', 1, rn + 1) is not null)
select *
from (
select key, rn, text_a, text_b,
case when a.word <> b.word then b.word end word
from a full join b using (key, rn))
pivot (max(word) for rn in (1 w1, 2 w2, 3 w3)) order by key
此查询显示前三个单词的比较,is也进行比较。如果字符串可能有不同的字数,则在正确处理空部分时,必须小心并修改大小写