Pivot queries are a frequent topic in the Tek-tips Oracle forums. Thanks to taupirho's helpful tip from last year, we know how to write them using the sys_connect_by_path function, which is available in Oracle 10g R1 and above. See thread759-1492961
Starting in Oracle 11g, release 2, the "listagg" function simplifies the SQL coding of pivot queries even more. I see this function in an 11g R2 new features article by Tom Kyte:
Quoting from the article:
Starting in Oracle 11g, release 2, the "listagg" function simplifies the SQL coding of pivot queries even more. I see this function in an 11g R2 new features article by Tom Kyte:
Quoting from the article:
Tom Kyte said:Oracle9i Database Release 1 introduced user-defined aggregate functions, and the first user-defined aggregate I wrote, called STRAGG, created a delimited list within a group (an aggregate—see It was about 100 lines of code and required an object type and an aggregate mapping function—lots of stuff.
When Oracle Database 10g Release 1 came out, we could use the new SYS_CONNECT_BY_PATH function to accomplish the same capability in pure SQL without having to resort to lots of procedural code. It was “pure SQL,” but it was complex.
Now, in Oracle Database 11g Release 2, we can accomplish the “stringing up of a list” very easily:
SQL> select deptno,
2 listagg( ename, '; ' )
3 within group
4 (order by ename) enames
5 from emp
6 group by deptno
7 order by deptno
8 /
DEPTNO ENAMES
--------- --------------------
10 CLARK; KING; MILLER
20 ADAMS; FORD; JONES;
SCOTT; SMITH
30 ALLEN; BLAKE;
JAMES; MARTIN;
TURNER; WARD