2

例如,我有两个表需要两个小输入表:-

Table1:-

columnA   
man got wounded by dog  
joe met sally  



Table2:-  
ColumnB  
life is good  
dog man got hunt   
dumb man wounded iron   

我想在columnA中搜索columnB中的一行,其中包含最大匹配单词,例如:-

Intermediate Output of above table should be:-
ColumnA    ColumnB    words_matching  number_of_words 
"man got wounded by dog"   "dumb man wounded iron"    "man,wounded"  2  
"man got wounded by dog"    "dog man got hunt"    "dog,man,got"   3  

在最终结果输出中,我想显示:-

ColumnA    ColumnB    words_matching  number_of_words 
"man got wounded by dog"    "dog man got hunt"    "dog,man,got"   3 

PS:-我只提供了一种情况的输出,表格会很大。也无法在列数据之间添加空格,因此使用引号。

我已经尝试使用层次结构查询来完成上述我打破字符串的操作,但这需要很多时间:- 我如何打破字符串的示例:-

select column1,regexp_substr(column1,'[^ ]+', 1, level) break_1 from table1
 connect by regexp_substr(column1,'[^ ]+', 1, level) is not null;

下面是我提出的另一个查询,但由于笛卡尔连接,性能非常低,因此对于海量数据来说这不是一个好主意:

 select st1,st2,
 max(round((extractvalue(dbms_xmlgen.getxmltype('select cardinality (
  sys.dbms_debug_vc2coll(''' || replace(replace(lower(st1),''''), ' ', ''',''' ) || ''') multiset intersect
  sys.dbms_debug_vc2coll('''||replace(replace(lower(st2),''''), ' ', ''',''' )||'''))  x from dual'), '//text()')),2))  seq
  from (
  select l1.column1 st1,l2.column2 st2
  from
  table1 l1,table2 l2 ) group by st1,st2;

有人可以提出一个好的方法 -

4

1 回答 1

1

我找到了解决上述问题的更快方法,正在使用过程来分解字符串并存储在不同的表中,然后使用这些表来查找匹配的字符串。

程序:-

create or replace
procedure split_string_word_match 
as 
type varr is table of  varchar(4000);
list1 varr;
list2 varr;

begin
select distinct column1 bulk collect into list1 from table1 ;
select distinct column2 bulk collect into list2 from table2 ;

for k in list1.first..list1.last
loop
insert into list1_result
select list1(k),regexp_substr(list1(k),'[^ ]+', 1, level) break_1 from dual
 connect by regexp_substr(list1(k),'[^ ]+', 1, level) is not null;
commit;
end loop;

for i in list2.first..list2.last
loop
insert into list2_result
select list2(i),regexp_substr(list2(i),'[^ ]+', 1, level) break_2 from dual
 connect by regexp_substr(list2(i),'[^ ]+', 1, level) is not null;
commit;
end loop;
end;
/

然后在结果表上使用下面的 sql 来找到最匹配的字符串:-(比过程中的许多循环工作得更快,因此写了一个 SQL)

select st1,st2,cs_string ,max(cnt) max_count
from (
select l1.column1 st1,l2.column2 st2,listagg(l1.break_1,',') within group(order by l1.break_1) cs_string ,count(1) cnt
from list1_result l1,list2_result l2
where l1.break_1 = l2.break_1
group by l1.column1,l2.column2)
group by st1,st2,cs_string;
于 2013-11-06T07:52:20.093 回答