【问题标题】:Oracle select mutual sub string from different rowsOracle 从不同的行中选择相互的子字符串
【发布时间】:2020-07-06 12:48:51
【问题描述】:

我想在 ORACLE 中返回新表,其中所有在 'col' 列中具有相同值的行组合在一起,并且当不同字符将替换为 '.. .'

我该怎么做?请问我可以得到你的帮助吗?

示例:

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

谢谢!

【问题讨论】:

  • 我今天早些时候已经看到了这篇文章。旧的怎么了?它被删除还是什么?This是帖子。请在此处提出您的进一步疑问,而不是打开另一个帖子。
  • 嗨,Mandy,我在上一篇文章中选择了不好的例子,所以我再次发布了一个用户问我的问题。这是原始帖子的链接:stackoverflow.com/questions/62754557/…
  • 您可以在那里编辑,没有任何问题;直到和除非您已接受答案,然后尝试重新编辑。
  • 是的,我这样做了,因此添加了:“示例 2”,但有人告诉现在这是一个不同的问题
  • @Mandy8055 。 . .这相同。数据完全不同,因为需要估算结果。 SQL 不是解决此问题的正确工具。

标签: sql regex oracle substring


【解决方案1】:

为了实现你想要的,首先你需要知道如何在 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

当然,您需要将它应用到您的表中。区别:

  • 您需要使用您的列
  • 您需要按 col 分组

【讨论】:

    猜你喜欢
    • 2020-10-26
    • 1970-01-01
    • 2020-07-25
    • 2021-12-10
    • 2014-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-12
    相关资源
    最近更新 更多