Thursday, April 9, 2009

Use xmlagg in Oracle to concat multiple rows into String

Or in other words, Pivot the values from the results of a query.

The following article contains a lot of information about this problem that we usually face:
http://tkyte.blogspot.com/2007/03/stringing-them-up.html

Read the comments section.

This is the most intuitive solution that I prefer:

SELECT deptno,
       SUBSTR (REPLACE (REPLACE (XMLAGG (XMLELEMENT ("x", ename)
                                 ORDER BY ename),'</x>'),'<x>','|'),2)  as concated_list
FROM emp
    GROUP BY deptno
    ORDER BY deptno;




1 comment:

Anonymous said...

I don't know who you are, or if you still maintain this site... But thank you. After days of searching you have the simplest explanation on how to do this. Everyone kept pointing back to this article and I couldn't use those solutions for various reasons

http://www.oracle-base.com/articles/misc/string-aggregation-techniques.php