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

Brand     Price   Colour  Size
addidas   500     Black    7
Nike      400     Blue     8
Reebok    200     Red      9


I need to transpose it like:

AttributeName    AttributeValue

Brand                addidas
Price                 500
Colour                Black
Size                   7
Brand                 Nike
Price                 400
Colour                Blue
Size                   8
Brand                 Reebok
Price                  200        
Colour                 Red
Size                    9


What I have tried:

No source still found only found to transpose to row to column vice solutions
Posted
Updated 1-Feb-18 9:09am
v2

1 solution

Hello,
As per your requirement I make below solution.Though it is not good approach.
//Create Table
CREATE TABLE a
(
  id int,
  brand varchar(20) ,
  price int,
  color varchar(20),
  size int
) ;

//Insert rows into table
INSERT INTO a (id,brand,price,color,size)
VALUES 
(1,'addidas', 500 ,'Black', 7),
(2,'Nike', 400 ,'Blue' ,8),
(3,'Reebok', 200 ,'Red' ,6) ;


Query to get the required output is
select  AttributeName,AttributeValue from 
(select a.id,1 as 'colid','brand' as 'AttributeName', brand as 'AttributeValue' from a
union 
select a.id,2 as 'colid','price' , price from a
union
select a.id,3 as 'colid','color',color from a
union 
select a.id ,4 as 'colid','size',size from a
)x order by x.id,x.colid

Here colid variable is used to get the attribute value by required order.
Out put is available in This link
Thanks
 
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