当前位置: 代码迷 >> DB2 >> db2 行列转换有关问题, !请高来指教下
  详细解决方案

db2 行列转换有关问题, !请高来指教下

热度:9994   发布时间:2013-02-26 00:00:00.0
db2 行列转换问题,急 !请高来指教下
现在有这样一个表 

字段 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);
  相关解决方案