Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Pivot Unpivot

Posted on 2008-10-04
3
Medium Priority
?
910 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
[X]
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
  • 2
3 Comments
 
LVL 60

Accepted Solution

by:
Kevin Cross earned 2000 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 60

Expert Comment

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

You are most welcome.
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

There are some very powerful Dynamic Management Views (DMV's) introduced with SQL 2005. The two in particular that we are going to discuss are sys.dm_db_index_usage_stats and sys.dm_db_index_operational_stats.   Recently, I was involved in a di…
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.
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

688 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