Solved

create pivot

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

     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 …
After restoring a Microsoft SQL Server database (.bak) from backup or attaching .mdf file, you may run into "Error '15023' User or role already exists in the current database" when you use the "User Mapping" SQL Management Studio functionality to al…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

920 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

13 Experts available now in Live!

Get 1:1 Help Now