Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 924
  • Last Modified:

Pivot Unpivot

How do i pivot/unpivot the follwoing result

month column1 column2
----- ------- -------
jan     1            33      
feb            3            33      
mar            33            22      

to

columns            jan            feb            mar
column1            1            3            33
colum2            33            33            22
0
suryajandhyala
Asked:
suryajandhyala
  • 2
1 Solution
 
Kevin CrossChief Technology OfficerCommented:
+First you have to get column1 and column2 in your data.
+Then pivot by the month column which will group on the column names
(this requires hardcoded column names for JAN, FEB, MAR, ...)


;WITH myCTE AS (
SELECT 'Column1' AS Columns, [month], column1 AS [value]
UNION ALL
SELECT 'Column2', [month], column2
)
SELECT columns,[jan],[feb],[mar]
FROM myCTE
PIVOT (SUM([value]) FOR [month] IN ([jan],[feb],[mar])) p

Open in new window

0
 
suryajandhyalaAuthor Commented:
Thanks a lot.
0
 
Kevin CrossChief Technology OfficerCommented:
>>Grading Comments: Thanks a lot.

You are most welcome.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now