Monday, April 22, 2013

How to covert rows to columns

How to covert rows to columns:

We have created a sample table for this exercise:

CREATE TABLE SCHEMA.column_to_row (line# int, word varchar(100));
insert into SCHEMA.column_to_row values (1,'The'), (1,'Horse'),(1,'is'), (1,'white');
insert into SCHEMA.column_to_row values (2,'The'), (2,'Rose'),(2,'is'), (2,'Red');
select * from SCHEMA.column_to_row with ur;

You can use XML elements for converting rows to columns:
 
SELECT line#, replace(replace(xml2clob(xmlagg(xmlelement(NAME a, s.word))),'<A>',''),'</A>',' ,')
FROM SCHEMA.column_to_row s
where line#=1
GROUP BY line#




No comments:

Post a Comment