Solved

create pivot

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

747 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

12 Experts available now in Live!

Get 1:1 Help Now