代码改变世界

DB2多行转一行【XML方式】

2017-04-23 17:21  tlnshuju  阅读(939)  评论(0编辑  收藏  举报
分组然后合并,然后去除XML标签
SELECT replace(replace(replace(xml2clob(xmlagg(xmlelement(name A, 【字段】))),'</A><A>', '【分隔符】'),'</A>',''),'<A>','')   
from 【表】
group by 【分组字段】


如:

SELECT replace(replace(replace(xml2clob(xmlagg(xmlelement(name A, nounid))),'</A><A>', ','),'</A>',''),'<A>','')     
from SYS_NOUN
group by nounsuper

这样的方式执行速度非常快。