Click here to Skip to main content
15,890,527 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am Having a Result Set Like This

ItemCode Value1 Value2 Value3 Value4

Item1 1 2 3 4
Item2 5 6 7 8
Item3 9 10 11 12


I need to convert this table to following format

Item1 Item2 Item3
1 5 9
2 6 10
3 7 11
4 8 12


How to do this??
Posted
Updated 28-Oct-12 20:10pm
v2
Comments
damodara naidu betha 29-Oct-12 3:34am    
hi. Is Values Columns count fixed?

1 solution

SQL
select * from (select ItemCode,Value1  from tbl_test) AS SourceTable
PIVOT
(
    max(Value1)
FOR ItemCode IN ([Item1],[Item2],[Item3])
) AS PivotTable
Union
select * from (select ItemCode,Value2  from tbl_test) AS SourceTable
PIVOT
(
    max(Value2)
FOR ItemCode IN ([Item1],[Item2],[Item3])
) AS PivotTable
Union
select * from (select ItemCode,Value3  from tbl_test) AS SourceTable
PIVOT
(
    max(Value3)
FOR ItemCode IN ([Item1],[Item2],[Item3])
) AS PivotTable
Union
select * from (select ItemCode,Value4  from tbl_test) AS SourceTable
PIVOT
(
    max(Value4)
FOR ItemCode IN ([Item1],[Item2],[Item3])
) AS PivotTable;
 
Share this answer
 
Comments
sruthyharidas 31-Oct-12 2:41am    
Thanks a lot .. .It worked

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900