Solved

create pivot

Posted on 2014-02-17
3
215 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
[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 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

     When we have to pass multiple rows of data to SQL Server, the developers either have to send one row at a time or come up with other workarounds to meet requirements like using XML to pass data, which is complex and tedious to use. There is a …
SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
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…

752 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