Custom Search
Logiclabz
  • Home
  • Sql Server
  • Transpose Columns to Rows in Sql Server 2005 using UNPIVOT

Transpose Columns to Rows in Sql Server 2005 using UNPIVOT

  

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
Transpose Column to rows using UNPIVOT

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

Transpose Column to rows using UNPIVOT

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.



  


Leave a reply


Comments

  • amit says:
    May 21, 09

    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...

  • logiclabz says:
    May 27, 09

    I have post a new article check it out Transpose Rows to Columns in Sql Server 2005 using PIVOT

  • haakondahl says:
    Mar 04, 10

    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.

  • Kwex says:
    Sep 09, 10

    Thanks a bunch ... this was very helpful!

  • ganez says:
    Oct 28, 10

    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

  • sgogu says:
    Jan 10, 11

    Thanks so much for the code. The sql explanation did help a lot

  • Raza says:
    Jan 11, 11

    Very nice piece of work, really helpful, thanks

  • tno2007 says:
    Feb 03, 11

    wow just what i was looking for thanks alot!!

  • Sindhu says:
    Jun 22, 11

    Thanks a lot!! It helped me a lot!!

  • zubin joshi says:
    Jul 18, 11

    if columns type are different how to handle this

  • zubin joshi says:
    Jul 18, 11

    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;

  • Hirel says:
    Sep 22, 11

    AWESOME!! Thnx a ton

  • Nguy?n6 Luân says:
    Oct 09, 11

    C?m on r?t nhi?u

  • gus says:
    Oct 10, 11

    i officially love you

  • supriatna says:
    Nov 01, 11

    help me a lot, thanks

  • samuel says:
    Dec 12, 11

    "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,



Do you like this post?