Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
dprichard
Yak Posting Veteran
94 Posts |
Posted - 2009-01-12 : 15:04:31
|
| I have a table with the following columns:ProductID - Sport - Color - Sizean example of the data in this would be1001 - Soccer - Red - Large1002 - Basketball - Blue - SmallI am trying to show the results like this in a separate viewProductID - Class - Option1001 - Sport - Soccer1001 - Color - Red1001 - Size - Large1002 - Sport - Basketball1002 - Color - Blue1002 - Size - SmallAny help would be greatly appreciated. |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
dprichard
Yak Posting Veteran
94 Posts |
Posted - 2009-01-12 : 16:53:22
|
| Sorry, SQL 2005 |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-01-12 : 23:47:20
|
| [code]SELECT *FROM(SELECT * FROM Table)tUNPIVOT(Val FOR Category IN ([Sport],[Color],[Size]))u[/code] |
 |
|
|
sodeep
Master Smack Fu Yak Hacker
7174 Posts |
Posted - 2009-01-12 : 23:51:54
|
| SELECT ProductID, UNP.Class, UNP.[Option] FROM (SELECT CONVERT(sql_variant,ProductID)as ProductID,CONVERT(sql_variant,Sport)as Sport,CONVERT(sql_variant,Color)as Color,CONVERT(sql_variant,[Size])as [Size] FROM Table)d UNPIVOT ([Option] For Class In (Sport, Color, [Size])) as UNP |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-01-12 : 23:52:20
|
| [code]declare @Table table(ProductID int, Sport varchar(20), Color varchar(20), [Size] varchar(20))insert into @tableselect 1001,'Soccer','Red','Large' union allselect 1002 ,'Basketball','Blue','Small'select ProductID,category,valfrom(select * from @table) tunpivot(val for category in ([Sport],[Color],[Size]))uoutput---------------------------------------ProductID category val1001 Sport Soccer1001 Color Red1001 Size Large1002 Sport Basketball1002 Color Blue1002 Size Small[/code] |
 |
|
|
|
|
|
|
|