Solved

Pivot Unpivot

Posted on 2008-10-04
3
888 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

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to automate & schedule this Index optimization Script ? 10 76
Need some help wiht :CAST AS Double 11 43
SQL Merige returns error code when updating 15 51
sql query help 2 43
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.
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

929 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now