如何仅使用 Oracle SQL return 2 个字符串之间的差异
How to return the difference between 2 strings using Oracle SQL only
我有 2 个字符串,例如:
- 'Source:Siebel; Name:Mary Jane; Gender:F; Age:24; N;'
- 'Source:Siebel; Name:Marie; Gender:F; Age: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)
来自
因为结果是相似的
2 个字符串的 [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>
The result I need is:
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
这将为您提供与其他字符串不匹配的字符串、姓名、年龄、性别等任何属性之间的差异。
我有 2 个字符串,例如:
- 'Source:Siebel; Name:Mary Jane; Gender:F; Age:24; N;'
- 'Source:Siebel; Name:Marie; Gender:F; Age: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)
来自
因为结果是相似的 2 个字符串的 [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>
The result I need is:
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
这将为您提供与其他字符串不匹配的字符串、姓名、年龄、性别等任何属性之间的差异。