如何仅使用Oracle SQL返回两个字符串之间的差异

如何仅使用Oracle SQL返回两个字符串之间的差异,sql,string,oracle,difference,oracle-fusion-apps,Sql,String,Oracle,Difference,Oracle Fusion Apps,我有两个字符串,例如: 资料来源:Siebel;姓名:玛丽·简;性别:F;年龄:24岁;;N、 " 资料来源:Siebel;姓名:玛丽;性别:F;年龄:24岁;;N、 " 我需要的结果是: 姓名:玛丽·简; 姓名:玛丽; 最有可能的是,我需要反转下面的代码 with cte1 as ( select 1 id, 'Source:Siebel; Name:Mary Jane; Gender:F; Age:24; N;' str from dual union all se

我有两个字符串,例如:

资料来源:Siebel;姓名:玛丽·简;性别:F;年龄:24岁;;N、 " 资料来源:Siebel;姓名:玛丽;性别: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)

结果就是相似性 两个字符串中的[QueryResult]

我无法使用此过程,因为我需要此SQL脚本在Oracle Fusion中运行这有帮助吗

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>
这有帮助吗

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>
我需要的结果是:

您可以使用滞后/超前分析功能来获得所需的输出

具有多个输入值的演示,例如“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
这将为您提供字符串中与其他字符串不匹配的任何属性、名称、年龄、性别等之间的差异

我需要的结果是:

您可以使用滞后/超前分析功能来获得所需的输出

具有多个输入值的演示,例如“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

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

为什么数据以该格式开头?即使它来自只输出复杂字符串的源,当您在数据库中导入数据时,也应该首先对其进行规范化。您好,我使用了该示例数据,因为我使用的实际数据是来自Oracle接口表和基表的连接值。我正在比较这两个结果,以确保接口表中的数据被传递到基表,我不确定我是否理解。数据要么是从接口传递到基表,要么不是;我不知道在这个过程中玛丽·简怎么会变成玛丽·简。将数据从一个地方复制或传输到另一个地方可能会在许多方面失败,但我认为更改数据内容的情况非常罕见。为什么数据一开始就采用这种格式?即使它来自只输出复杂字符串的源,当您在数据库中导入数据时,也应该首先对其进行规范化。您好,我使用了该示例数据,因为我使用的实际数据是来自Oracle接口表和基表的连接值。我正在比较这两个结果,以确保接口表中的数据被传递到基表,我不确定我是否理解。数据要么是从接口传递到基表,要么不是;我不知道在这个过程中玛丽·简怎么会变成玛丽·简。将数据从一个地方复制或传输到另一个地方可能会在许多方面失败,但我认为更改数据内容的情况非常罕见。嘿!我想这会管用的。我只需要把它和我的整个脚本结合起来。如果OPs示例中有两行或两个以上的字符串,那么这将不起作用。关键字是if。据我们所知,没有!我想这会管用的。我只需要把它和我的整个脚本结合起来。如果OPs示例中有两行或两个以上的字符串,那么这将不起作用。关键字是if。据我们所知,没有。