oracle sql根据条件连接2列

ddrv8njm  于 2021-07-24  发布在  Java
关注(0)|答案(1)|浏览(399)

假设我有一张table:

+----+------------+------------+
| ID |   NAME1    |     NAME2  |
+----+------------+------------+
| A1 |    DAVE    |    DAN     |
| A1 |    DOR     |    MON     |
| A2 |    STEPHAN |    ARKEL   |
| A3 |    ASH     |    CATCHAM |
| A4 |    RON     |    DON     |
| A4 |    OFIR    |    DOL     |
| A4 |    OFRA    |    SOL     |
+----+------------+------------+

我想根据id连接,如果下一行具有相同的id,则添加两个名称,如下所示:

+----+-----------------------------------+
| ID |             NEW_NAME              |
+----+-----------------------------------+
| A1 |    DAVE~~DAN^^DOR~~MON            |
| A2 |    STEPHAN~~ARKEL                 |
| A3 |    ASH~~CATCHAM                   |
| A4 |    RON~~DON^^OFIR~~DOL^^OFRA~~SOL |
+----+-----------------------------------+

谢谢。

ax6ht2ek

ax6ht2ek1#

你可以做:

select
  id,
  listagg(name1 || '~~' || name2, '^^') 
    within group (order by name1, name2) as new_name
from t
group by id

请参见在sql<>fiddle上运行示例。

相关问题