Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Pivot Unpivot

Posted on 2008-10-04
3
896 Views
Last Modified: 2010-04-21
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
Comment
Question by:suryajandhyala
  • 2
3 Comments
 
LVL 59

Accepted Solution

by:
Kevin Cross earned 500 total points
ID: 22642582
+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
 

Author Closing Comment

by:suryajandhyala
ID: 31503107
Thanks a lot.
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 22642789
>>Grading Comments: Thanks a lot.

You are most welcome.
0

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you having speed problem in loading SQL Server Management Studio, try to uncheck these options in your internet browser (IE -> Internet Options / Advanced / Security):    . Check for publisher's certificate revocation    . Check for server ce…
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question