【问题标题】:Remove duplicate values from string in oracle在oracle中从字符串中删除重复值
【发布时间】:2020-05-04 10:37:28
【问题描述】:

我有输入数据的要求,例如

Col1 COl2 Col3
A1    2    B
A1    1    A
A1    3    B
B1    1    A
B2    2    B
B2    3    C
B4    4    C
B5    5    A
B6    6    B

需要输出:

Col1 COl2 Col3
A1    2    AB
A1    1    AB
A1    3    AB
B1    1    ABC
B2    2    ABC
B2    3    ABC
B4    4    ABC
B5    5    ABC
B6    6    ABC    

已尝试的解决方案:

select col1,col2,listagg(col3,'') within group (order by col3) over(partition by col1)
from tab

查询的输出:

Col1 COl2 Col3
A1    2    ABB
A1    1    ABB
A1    3    ABB
B1    1    AABBCC
B2    2    AABBCC
B2    3    AABBCC
B4    4    AABBCC
B5    5    AABBCC
B6    6    AABBCC

有人可以在这里帮忙删除重复的字母吗?

谢谢

【问题讨论】:

  • 类似SELECT Col1, Col2 LEFT JOIN (SELECT DISTINCT Col1, Col3 ...) BY Col1。在 SELECT DISTINCT 子查询中是您进行聚合的地方。

标签: sql oracle11g oracle18c


【解决方案1】:

您可以使用子查询:

select col1, col2,
       listagg(case when seqnum = 1 then col3 end, '') within group (order by col3) over (partition by col1)
from (select t.*,
             row_number() over (partition by col1, col3 order by col3) as seqnum
      from tab t
     ) t

【讨论】:

  • 简单易行的解决方案。
猜你喜欢
  • 2021-04-13
  • 2013-08-30
  • 2016-05-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-08
  • 1970-01-01
相关资源
最近更新 更多