Solved

create pivot

Posted on 2014-02-17
3
211 Views
Last Modified: 2014-02-17
Hello Experts,

How I can achieve my desired pivot?

Please refer to the attached image.

pivotsample

Thanks.
0
Comment
Question by:JimiJ13
  • 2
3 Comments
 
LVL 65

Accepted Solution

by:
Jim Horn earned 500 total points
ID: 39864709
Here you go.  Copy the below code and paste in SSMS, then execute, then modify to meet your needs.

Using PIVOT and UNPIVOT

Limitation: The 0-4 are hard-coded, so if you ever have more than 4 you'll have to manually add that to the T-SQL.
CREATE TABLE #pivot (id int, msg varchar(50))

INSERT INTO #pivot (id, msg) 
VALUES 
   (0, 'message content 1'), 
   (1, 'message content 2'), 
   (2, 'message content 3'), 
   (3, 'message content 4'), 
   (4, 'message content 5') 

SELECT 'Message Content' AS message_content, 
[0], [1], [2], [3], [4]
FROM
(SELECT id, msg
    FROM #pivot) AS pvt
PIVOT 
(
MIN(msg)
FOR id IN ([0], [1], [2], [3], [4])
) AS PivotTable;

Open in new window

0
 

Author Closing Comment

by:JimiJ13
ID: 39865727
Thank you for a quick solution and for pointing me to right reference.
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 39865760
Thanks for the grade.  Good luck with your project.  -Jim
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

Suggested Solutions

In this article I will describe the Detach & Attach 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.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

830 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