Friday, 26 May 2017

Converting Multiple Rows into a Single Column using XMLAGG


The following SQL code will convert the multiple rows to a single column using XMLAGG function. The output is same as LISTAGG function. 

  1. SELECT rtrim(XMLAGG(XMLELEMENT(Department,DEPARTMENT_NAME||',' ).EXTRACT('//text()')),',') DEPT_NAME FROM DEPARTMENTS;  
  2.   
  3. output  
  4. -------  
  5.   
  6. DEPARTMENT_NAME  
  7. -----------------------   
  8. Administration,Marketing,Purchasing,Human Resources,Shipping,IT,Public Relations,Sales,Executive,Finance,Accounting,Treasury,Corporate Tax,Control And Credit,Shareholder Services,Benefits,Manufacturing,Construction,Contracting,Operations,IT Support,NOC,IT Helpdesk,Government Sales,Retail Sales,Recruiting,Payroll 

No comments:

Post a Comment