oracle选择相互子字符串

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

我想在oracle中返回一个新表,其中所有在“col”列中具有相同值的行组合在一起,“description”列将只包含相互的子字符串,而不同的字符将替换为“…”
我该怎么做?我可以请你帮忙吗?
基本代码:选择列,从表中按列分组描述;
例1:

col description
1   Today is 
1   Today is a good day
1   Today is perfect day
2   Hello world
2   Hello

结果:

col description
1   Today is …
2   Hello…

例2:

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

谢谢!

8ehkhllq

8ehkhllq1#

这回答了问题的原始版本。
你可以用 not exists :

select col, description || ' ...'
from t
where not exists (select 1
                  from t t2
                  where t2.description like t.description || '%' and
                        t2.descriptoin <> t.description
                 );

注意,在一个大table上,这将是没有效率的!

相关问题