从oracle中的字符串中删除重复的值

问题描述 投票:0回答:1

我有要求输入像这样的输入数据的地方>

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

有人可以帮助您删除重复的字母。

谢谢

我有输入像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这样的输入数据的要求:...

sql oracle11g oracle-sqldeveloper plsqldeveloper oracle18c
1个回答
0
投票

您可以使用子查询:

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
© www.soinside.com 2019 - 2024. All rights reserved.