现在有这样一个表
字段 A B
1 a
1 b
1 c
2 d
2 e
现在要合并成
A B
1 a,b,c
2 c,d
请问有没有一个sql语句搞定的 或一个函数?
------解决方案--------------------------------------------------------
沒用過DB2
同MS SQL和Oracle CTE用法時,ID2是多於的,遞歸吧不需要再定義ID2
如:
- SQL code
with s as (select row_number()over(partition by iname order by id) id1, ivalue,iname from zxt_test),t(iname,id1,ivalue) as(select iname,id1,cast(ivalue as varchar(100)) from s where id1 =1union all select t.iname,s.ID1,cast(s.ivalue||','||t.ivalue as varchar(100)) from s, t where s.id1=t.id1+1 and t.iname = s.iname )select iname,ivalue from t where t.id1= (select max(id1) from s where s.iname = t.iname);