Oracle-通过串联重复的行联接两个表

时间:2019-02-05 16:43:14

标签: sql database oracle

我有2张这样的桌子:

table_a

id  col2
1   A
1   B
2   A
2   B
3   B

table_b

id  col1
1    X
2    Y
3    Z

结果:

id col1 col2
1   X    A_B
2   Y    A_B
3   Z    B

我该如何实现?

1 个答案:

答案 0 :(得分:3)

您要listagg()

select b.id, b.col1,
       listagg(a.col2, '_') within group (order by a.col2) as col2
from table_b b join
     table_a a
     on b.id = a.id
group by b.id, b.col1;