Oracle 如何将一列的重复值分组到另一列
Oracle how I can group by duplicate value of a column into an other column
我有一个 oracle VIEW,它在一个列中包含一些重复值我想将这些重复值分组到另一列中的另一列中,这是我的视图:
myView :
---------------------------
ID | col 1 | col 2 |
---------------------------
1 | P1 |V1
2 | P1 |V1
3 | P1 |V2
4 | P1 |V3
5 | P2 |t1
6 | P2 |t1
7 | P2 |t2
我想创建另一个基于 myView 的视图,它将包含如下值:
ID | col 1 | col 2 |col 3
---------------------------------------
1 | P1 |V1 |V1,V1,V2,V3
2 | P1 |V1 |V1,V1,V2,V3
3 | P1 |V2 |V1,V1,V2,V3
4 | P1 |V3 |V1,V1,V2,V3
5 | P2 |t1 |t1,t1,t2
6 | P2 |t1 |t1,t1,t2
7 | P2 |t2 |t1,t1,t2
提前感谢您的帮助
您可以将 listagg()
用作 window 函数:
select t.*,
listagg(col2, ',') within group (order by col1) over (partition by col1) as col3s
from t;
select id, col1, col2, LISTAGG(col2, ',') WITHIN GROUP (ORDER BY col1) OVER (PARTITION BY col1) as col3
from myView;
我有一个 oracle VIEW,它在一个列中包含一些重复值我想将这些重复值分组到另一列中的另一列中,这是我的视图:
myView :
---------------------------
ID | col 1 | col 2 |
---------------------------
1 | P1 |V1
2 | P1 |V1
3 | P1 |V2
4 | P1 |V3
5 | P2 |t1
6 | P2 |t1
7 | P2 |t2
我想创建另一个基于 myView 的视图,它将包含如下值:
ID | col 1 | col 2 |col 3
---------------------------------------
1 | P1 |V1 |V1,V1,V2,V3
2 | P1 |V1 |V1,V1,V2,V3
3 | P1 |V2 |V1,V1,V2,V3
4 | P1 |V3 |V1,V1,V2,V3
5 | P2 |t1 |t1,t1,t2
6 | P2 |t1 |t1,t1,t2
7 | P2 |t2 |t1,t1,t2
提前感谢您的帮助
您可以将 listagg()
用作 window 函数:
select t.*,
listagg(col2, ',') within group (order by col1) over (partition by col1) as col3s
from t;
select id, col1, col2, LISTAGG(col2, ',') WITHIN GROUP (ORDER BY col1) OVER (PARTITION BY col1) as col3
from myView;