oracle多行转一列
多行数据合并后,数据长度不超过4000,用以下sql:
select listagg(a.book_id,',') within group (order by a.book id) from tmp_123 a;
如果长度超过4000,会报 ora-01489:字符串连接的结果过长。用以下sql解决:
select xmlagg(xmlparse(content a.book_id||',’ wellformed) order by a.book_id).getclobval() from tmp_123 a;
或者
select rtrim(xmlagg(xmlelement(e, a.book_id,',').extract('//text()') order by a.book_id).getclobval(),' ') from tmp_123 a;
以上sql中的content
,wellformed
,e
,//text()
都是固定写法。