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
Updated 1-Feb-18 9:09am

1 solution

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

//Insert rows into table
INSERT INTO a (id,brand,price,color,size)
(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,1 as 'colid','brand' as 'AttributeName', brand as 'AttributeValue' from a
select,2 as 'colid','price' , price from a
select,3 as 'colid','color',color from a
select ,4 as 'colid','size',size from a
)x order by,x.colid

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