如何合并多行数据为一行数据
如何合并多行数据为一行数据
select * from data1 案件表
ID NAME
1 案件1
2 案件2
select * from data2 当事人表
ID NAME
1 当事人1
1 当事人2
2 当事人3
2 当事人4
案件表分为案件1和案件2,当事人表分为1234四个当事人,想要得到的结果为:
案件 当事人
案件1 当事人1、当事人2
案件2 当事人3、当事人4
SQL语句的写法如下:
select (SELECT max(substr(name,2)) name
FROM
(SELECT id,sys_connect_by_path(name,',') name
FROM
(SELECT id,name,id||rn rchild,id||(rn-1) 易做图ther
FROM
(SELECT d2.id ,d2.name,row_number() over (PARTITION BY d2.id ORDER BY d2.name) rn FROM data2 d2))
CONNECT BY PRIOR rchild=易做图ther START WITH 易做图ther LIKE '%0') where id=d1.id
GROUP BY id) as dsr,d1.name
from data1 d1