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

Transpose Rows to Columns in Sql Server 2005 using PIVOT

As known SQL Server 2000 need some dynamic sqls to convert rows to columns or vice versa. PIVOT and UNPIVOT relational operator did the job of converting rows to columns and columns to rows respectively.

Below is the sample that transpose values from rows to columns in sql server 2005 using PIVOT relational operator,

Sample input table

create table #temptable(colorname varchar(25),Hexa varchar(7),rgb varchar(1), rgbvalue tinyint) 
GO
insert into #temptable values('Violet','#8B00FF','r',139);
insert into #temptable values('Violet','#8B00FF','g',0);
insert into #temptable values('Violet','#8B00FF','b',255);
insert into #temptable values('Indigo','#4B0082','r',75);
insert into #temptable values('Indigo','#4B0082','g',0);
insert into #temptable values('Indigo','#4B0082','b',130);
insert into #temptable values('Blue','#0000FF','r',0);
insert into #temptable values('Blue','#0000FF','g',0);
insert into #temptable values('Blue','#0000FF','b',255);
GO
select * from #temptable
GO
Transpose rows to columns using PIVOT

The sample contains colors (Violet, Indigo, Blue) with its specific "R","G" & "B" values in each row. Our goal is to convert "R", "G" & "B" as column with specific value for each colors. This can be done using PIVOT. PIVOT uses a aggregate function to compile rows to columns so we not require to specify group by explicitly.

The T-SQL query to convert rows to columns is

SELECT colorname,hexa,[r], [g], [b]
FROM
(SELECT colorname,hexa,rgb,rgbvalue
    FROM #temptable) AS TableToBePivoted
PIVOT
(
sum(rgbvalue)
FOR rgb IN ([r], [g], [b])
) AS PivotedTable;

The output would be

Transpose rows to columns using PIVOT

In the above "SUM" aggregate function is used which would sum rgbvalue for same colorname,hexa mentioned in select statement. "rgb" column is tranfered to 3 columns namely "r, "g" & "b" using its value. syntax for understanding:

SELECT [<<unchanged columns>>],<<new columns for pivot>>
FROM
(SELECT [<<unchanged columns>>],<<source columns to be pivoted>>
    FROM <<tablename>>) AS <<alias name1>>
PIVOT
(
<<aggregate function>>(<<column with has values>>)
FOR <<source column to be pivoted>> IN (<<new columns for pivot>>)
) AS <<alias name2>>;

Thats it, hope it helps.


Comments

  • balu1293 says:
    Jun 01, 09

    I have search this concept for long time. it is very useful for me.

  • fahri says:
    Aug 28, 09

    I already tried to transpose rows to colums as your script but stiil not working. The condition just scripted as SELECT component.cmpnt_name TAG, hook_up_type.hu_type_name HUTYPE, hook_up.hu_name HUNAME FROM (((((component LEFT JOIN (component_mfr RIGHT JOIN component_mod ON component_mfr.cmpnt_mfr_id = component_mod.cmpnt_mfr_id) ON (component.cmpnt_mod_id = component_mod.cmpnt_mod_id) AND (component.cmpnt_mfr_id = component_mfr.cmpnt_mfr_id)) LEFT JOIN hook_up_component ON component.cmpnt_id = hook_up_component.cmpnt_id) LEFT JOIN hook_up ON hook_up_component.hu_id = hook_up.hu_id) LEFT JOIN hook_up_drawing_settings ON hook_up_component.hu_id = hook_up_drawing_settings.hu_id) LEFT JOIN drawing d3 ON hook_up_drawing_settings.dwg_id = d3.dwg_id) LEFT JOIN hook_up_type ON hook_up.hu_type_id = hook_up_type.hu_type_id could you help me to transpose base on my srcipt above

  • Sandeep Kumar says:
    Oct 22, 09

    Thnx for great help........

  • Anamika says:
    Nov 16, 09

    It was greatly useful in understanding the PIVOT functionality. Thanks a lot Buddy.

  • sandeep says:
    Feb 11, 10

    Hi.. I think this query was greate but one problem with my table ... rgbvalues in my table are varchar then it will gives an error like -- Operand data type nvarchar is invalid for sum operator.

  • Shaheena Mushtaq says:
    Mar 01, 10

    Hi thnks for the solution but it didnt wotked for me n i dont know y I used the same query n have just replaces my paramteres bcz i have the same condition for transposing 3 rows to 3 cols

  • Anandarajeshwaran.J says:
    Mar 24, 10

    Thanks for the article. I was struggling to make it work with a varchar. Finally I was able to make it work using "max" as an aggregate function. You saved ton of time for me

  • Allen says:
    Jun 24, 10

    Thank you so much for putting this together- your simple example made easy work of undertanding and implementing this for my needs! Thank you again!!

  • Subhashini says:
    Jun 29, 10

    thanks for the help... it was really useful


Leave a reply


Do you like this post?