首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > IBM DB2 >

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

2012-02-10 
db2 行列转换问题,急!请高来指教下现在有这样一个表字段AB     1  a1b1c2d2e现在要合并成AB1a,b,c2c,d请问

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); 

热点排行