Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a table as below

amber green red
11 89 0

How can I pivot this to

amber 11
green 89
red 0

What I have tried:

select u.name, u.value
from #temp s
unpivot
(
value
for color in (amber, green, red)
) u;
Posted
Updated 12-Aug-16 0:59am

Hey here is a ready juice for you just copy and paste to check.
Happy coding :)

SQL
select Final.[Values] as Name,Final.[Value]
from
(
select amber,green,red
from TestPivot 
) as pvt
unpivot (
		[Value] FOR [Values] IN (amber,green,red) 
) as Final


Output will be:
Name |Value
-----------------
amber | 11
green | 89
red | 0
 
Share this answer
 
you can also take the help of this link: Using PIVOT and UNPIVOT[^]
 
Share this answer
 
 
Share this answer
 
try this
SQL


C#
select Name,[Values] from 
(
  select amber,green,red from #temp
)s
unpivot
(
  [Values] for Name in ([amber],[green],[red])
)as Final
 
Share this answer
 

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