Wednesday, September 19, 2012

Multiple rows into a single line in 'Single Column Table'

Multiple rows into a single line in 'Single Column Table'

CREATE TABLE TESTINGEMP(
  dept NUMBER(10),
  empname VARCHAR2(30));
INSERT INTO testingemp
     VALUES (10, 'THAMBI');
 
INSERT INTO testingemp
     VALUES (10, 'PETER');
 
INSERT INTO testingemp
     VALUES (10, 'ANTHONY');
 
INSERT INTO testingemp
     VALUES (20, 'GEORGE');
 
INSERT INTO testingemp
     VALUES (20, 'MICHAEL');
 
 
select * from testingemp
 
SELECT   dept,
         RTRIM (XMLAGG (XMLELEMENT (e, empname || ',')).EXTRACT ('//text()'),
                ','
               ) empnames
    FROM testingemp
GROUP BY dept

SELECT dept,listagg (empname, ',') WITHIN GROUP (ORDER BY empname)
        empnames
FROM testingemp
GROUP BY dept

SELECT  listagg (empname, ',') WITHIN GROUP (ORDER BY empname) employeenames
FROM testingemp

select rtrim (xmlagg (xmlelement (e, empname || ',')).extract ('//text()'), ',') Empnames
from testingemp