Click here to Skip to main content
15,898,982 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi
I have database in structure like below

Newspaper   State1  state2  state3  state4 

ABC          1000     5684   5687   246
DEF           879     2547   2578   2357

But I need to the structure like below

How can i change this 

Newspaper        State      copies

ABC              State1     1000
ABC              State2     5684
ABC              State3     5687
ABC              State4     246
DEF              State1     879
DEF              State2     2547
DEF              State3     2578
DEF              State4     2357

Pls help me

Maideen
Posted

1 solution

Hi check this below query works fine
SQL
SELECT [NewsPaper]
      ,[State],[Copies]
FROM 
   (SELECT [NewsPaper]
      ,[State1]
      ,[State2]
      ,[State3]
      ,[State4]
   FROM [Test].[dbo].[News]) p
UNPIVOT
   (Copies FOR State IN 
      (State1,State2,State3,State4)
)AS unpvt;
GO
 
Share this answer
 
Comments
Maideen Abdul Kader 26-Jun-14 4:05am    
Thank you Vani.
it is work.
vani suresh 26-Jun-14 9:57am    
Welcome..

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