oracle从不同的行中选择相互子字符串

mlmc2os5  于 2021-07-24  发布在  Java
关注(0)|答案(1)|浏览(349)

我想在oracle中返回一个新表,其中“col”列和“description”列中具有相同值的所有行将只包含相互的子字符串,而不同的字符将替换为“…”
我该怎么做?我可以请你帮忙吗?
例子:

col description
1   Today is a good day
1   Today is perfect day
2   Hello world
2   Hello I'm here
3   Hi

结果:

col description
1   Today is …
2   Hello…
3   Hi

谢谢!

gpfsuwkq

gpfsuwkq1#

为了实现您想要的目标,首先您需要知道如何在oracle中找到公共字符串开头:

with rws as ( 
  -- generate rows up to the length of the longest string 
  select rownum r from dual 
  connect by level <= (select max(length(str)) from strings) 
) 
select distinct s  
from ( 
  select str, max(subp) s  
  from ( 
    select str,  
           substr(str, 1, rws.r) subp 
    from   strings s1 
    cross join rws 
    where  r < length(str) 
    and    exists ( 
      -- check whether there's another string matching the first N chars 
      select * from strings s2 
      where  s1.str <> s2.str 
      and    substr(s1.str, 1, rws.r) = substr(s2.str, 1, rws.r) 
    ) 
  )  
  group by str 
)

取自https://livesql.oracle.com/apex/livesql/file/content_cc83zwpcpesedtadifosb1exi.html
当然,您需要将此应用于您的表。差异:
您将需要使用您的列
你需要按列分组

相关问题