SQL查询在列中查找重复?

时间:2017-11-03 19:57:22

标签: sql oracle

以下是我需要查询的数据的子集。 “我需要做的就是 采取任何匹配但仍有差异的路由并连接值 在一个单独的专栏中。前后见下文。有关如何的任何建议 这样做会很棒吗?

这是我的疑问:

select mcrp27, irkitl
from f0006
join f3003
on mcmcu = irmcu 
where mcrp27 <> ' '
and irmmcu = ' 20101556'
order by mcrp27

在:

ROUTING DEPT
======= =======  
R335700 1
R335800 1
R335900 1
R336000 1
R336100 1
R336200 1
R336300 1
R336500 1
R435500 1
R435501 1
R435600 1
R435700 1
R436400 1
R336100 2
R336200 2
R336300 2
R436400 2
R436400 3
R335300 5
R335300 6
R331400 7
R331500 7
R331600 7
R331900 7
R332100 7
R332200 7
R332400 7
R332500 7
R332600 7
R332700 7

后:

R335700 1   
R335800 1   
R335900 1   
R336000 1   
R336100 1   2   
R336200 1   2   
R336300 1   2   
R336500 1   
R435500 1   
R435501 1   
R435600 1   
R435700 1   
R436400 1   2   3
R335300 5   6   
R331400 7   
R331500 7   
R331600 7   
R331900 7   
R332100 7   
R332200 7   
R332400 7   
R332500 7   
R332600 7   
R332700 7   

1 个答案:

答案 0 :(得分:0)

由于每个ROUTING可以有任意数量的DEPT,并且SQL查询只能返回先前已知数量的列,因此我建议以逗号分隔的字符串返回DEPT。所以你有两列:ROUTING和DEPTS。

select routing, listagg(dept, ',') within group (order by dept) as depts
from mytable
group by routing
order by routing;