1

例如,我有 2 个字符串:

  • '来源:西贝尔;姓名:玛丽·简;性别:F;年龄:24;N;'
  • '来源:西贝尔;姓名:玛丽;性别:F;年龄:24;N;'

我需要的结果是:

  • 姓名:玛丽·简;
  • 姓名:玛丽;

很可能我需要反转下面的代码

with cte1 as  (
    select 1 id, 'Source:Siebel; Name:Mary Jane; Gender:F; Age:24; N;' str from dual
    union all
    select 2 id, 'Source:Siebel; Name:Marie; Gender:F; Age:24; N;' str from dual
), cte2 as (
    SELECT distinct id, trim(regexp_substr(str, '[^ ]+', 1, level)) str
    FROM cte1 t
    CONNECT BY instr(str, ' ', 1, level - 1) > 0
)
select distinct t1.str
from cte2 t1
join cte2 t2 on (t1.str = t2.str and t1.id != t2.id)

Oracle 函数返回字符串之间的相似性

因为结果是 2 个字符串的相似性 [QueryResult]

在此处输入图像描述

我无法使用该过程,因为我需要此 SQL 脚本才能在 Oracle Fusion 中运行

4

2 回答 2

0

我需要的结果是:

Name:Mary Jane; 
Name:Marie;

您可以使用LAG/LEAD 分析功能来获得所需的输出。

具有多个输入值的演示,例如'Mary Jane'、'Marie'、'Jane'、'Jones'

with t1 as  (
    select 1 id, 'Source:Siebel; Name:Mary Jane; Gender:F; Age:24; N;' str from dual
    union all
    select 2 id, 'Source:Siebel; Name:Marie; Gender:F; Age:24; N;' str from dual
    union all
    select 3 id, 'Source:Siebel; Name:Jane; Gender:F; Age:24; N;' str from dual
    union all
    select 4 id, 'Source:Siebel; Name:Jones; Gender:F; Age:24; N;' str from dual
), t2 as (
SELECT t1.id,
        trim(regexp_substr(t1.str, '[^;]+', 1, lines.column_value)) str
    FROM t1,
      TABLE (CAST (MULTISET
      (SELECT LEVEL FROM dual
              CONNECT BY instr(t1.str, ';', 1, LEVEL) > 0
      ) AS sys.odciNumberList ) ) lines
    ORDER BY id, lines.column_value)
select id, str from(
  select id, 
         str, 
        lag(str) over(partition by str order by str) lag, 
        lead(str) over(partition by str order by str) lead from t2
) where lag is null
  and   lead is null
order by id;

        ID STR
---------- -----------------------
         1 Name:Mary Jane
         2 Name:Marie    
         3 Name:Jane     
         4 Name:Jones

这将为您提供与其他字符串不匹配的字符串、姓名、年龄、性别等任何属性之间的区别。

于 2020-04-10T14:25:00.400 回答
0

这会有帮助吗?

SQL> with cte1 as  (
  2   select 1 id, 'Source:Siebel; Name:Mary Jane; Gender:F; Age:24; N;' str from dual
  3   union all
  4   select 2 id, 'Source:Siebel; Name:Marie; Gender:F; Age:24; N;' str from dual
  5   ),
  6  cte2 as
  7    (select id,
  8       column_value lvl,
  9       trim(regexp_substr(str, '[^;]+', 1, column_value)) str
 10     from cte1 cross join
 11       table(cast(multiset(select level from dual
 12                           connect by level <= regexp_count(str, ';') +1
 13                          ) as sys.odcinumberlist))
 14    )
 15  select a.str, b.str
 16  From cte2 a join cte2 b on a.id < b.id and a.lvl = b.lvl and a.str <> b.str;

STR             STR
--------------- ---------------
Name:Mary Jane  Name:Marie

SQL>
于 2020-04-10T12:45:43.050 回答