In Sql Server 2000, it was very hard converting column based values to transpose into row based values and vice versa.
PIVOT and UNPIVOT did the trick in Sql server 2005.
Below is the sample that transpose values from column to rows in sql server using UNPIVOT relational operator.
Sample input table
create table #temptable(rowid int,colorname varchar(25),Hexa varchar(7) ,R tinyint,G tinyint,B tinyint) GO insert into #temptable values(1,'Violet','#8B00FF',139,0,255); insert into #temptable values(2,'Indigo','#4B0082',75,0,130); insert into #temptable values(3,'Blue','#0000FF',0,0,255); insert into #temptable values(4,'Green','#00FF00',0,255,0); insert into #temptable values(5,'Yellow','#FFFF00',255,255,0); insert into #temptable values(6,'Orange','#FFA500',255,165,0); insert into #temptable values(7,'Red','#FF0000',255,0,0); GO select * from #temptable GO
The sample table contains colorname with "R","G" & "B" column with specific values.
Our goal is to convert "R","G" & "B" as rows with specific values in single column.
Main requirement of transposing columns to rows is to have identical datatypes of converting columns.
In the sample the datatype of "R", "G" & "B" being tinyint.
The T-SQL query to convert columns to rows is
SELECT rowid,colorname,hexa,rgb,rgbvalue FROM (SELECT rowid,colorname,hexa,r,g,b FROM #temptable) p UNPIVOT (rgbvalue FOR rgb IN (r,g,b)) AS unpvt;
The output would be
In the above sample "rowid,colorname,hexa" columns remains unchanged. "R", "G" & "B" columns are converted to rows with new column "rgb" and its specific values are added to new column "rgbvalue".
syntax for understanding:
select [<<unchanged columns>>],<<new column>>,<<new column for its values>> from ( select [<<unchanged columns>>],<<columns to be converted to rows>> from <<tablename>> ) <<alias name1>> UNPIVOT ( <<new column for its values>> FOR <<new column>> IN (<<columns to be converted to rows>>) ) AS <<alias name2>>
Thats it, hope it helps.
I want to transpose just reverse of ur example. Before transpose the data i want to show and after transpose data i have with me...
I have post a new article check it out Transpose Rows to Columns in Sql Server 2005 using PIVOT
Thank you. This is the first example using UNPIVOT which includes a simple example, before and after tables, and a syntax ref block along with the practical. This got the job done.
Thanks a bunch ... this was very helpful!
thx your query really helpful to me but how to do if i m having five cols rowid, prodid1, qty1, prodid2, qty2 to rowid, prodid, qty
Thanks so much for the code. The sql explanation did help a lot
Very nice piece of work, really helpful, thanks
wow just what i was looking for thanks alot!!
Thanks a lot!! It helped me a lot!!
if columns type are different how to handle this
Got workaround SELECT rowid,colorname,hexa,rgb,rgbvalue FROM (SELECT rowid,colorname,hexa,CAST(r as VARCHAR) r ,CAST(g as VARCHAR)g , CAST(b as VARCHAR) b FROM #temptable) p UNPIVOT (rgbvalue FOR rgb IN (r,g,b)) AS unpvt;
AWESOME!! Thnx a ton
C?m on r?t nhi?u
i officially love you
help me a lot, thanks
"Hi, nice query for pivot/transpose when using only 3 fields "R G B" but if you have more than 3 fields? any suggestions. My issue is I have table with 3 columns: (id, question, answer) and I want to show the questions in rows (cold be 100 question) and each answer will be also in rows under the related question. Will appreciate any suggestions. Thanks Thanks,