oracle SQL 根据条件连接2列
oracle SQL Concatenate 2 columns based on condition
假设我有一个 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 |
+----+-----------------------------------+
谢谢。
你可以这样做:
select
id,
listagg(name1 || '~~' || name2, '^^')
within group (order by name1, name2) as new_name
from t
group by id
请参阅 SQL<>Fiddle 中的 运行 示例。
假设我有一个 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 |
+----+-----------------------------------+
谢谢。
你可以这样做:
select
id,
listagg(name1 || '~~' || name2, '^^')
within group (order by name1, name2) as new_name
from t
group by id
请参阅 SQL<>Fiddle 中的 运行 示例。