Click here to Skip to main content
16,001,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
date	        sess	code	course
14-Jan-13	1	CM	AFF
14-Jan-13	3	CM	AFF
17-Jan-13	1	NR	CTF


i want the output as follows in sql server using query,how can i do please reply.it is very helpful for me.

               AFF (course)
        1   2  3  4 (sess)
14-Jan-13   CM     CM


              CTF  (course)
        1   2  3  4 (sess)
17-Jan-13   NR


I want the above output, using query.

please reply.it is very helpful for me.
Posted
Updated 19-Jan-13 2:18am
v2

check the following query
SQL
SELECT *
FROM (
SELECT *
FROM [YOUR_TABLE_NAME]
) P
PIVOT (
MAX([code])
FOR [sess] IN ([1],[2],[3] ,[4])
) AS PVT


It returns the output as follows

date       course 1     2     3     4
---------- ------ ----- ----- ----- -----
2013-01-14 AFF    CM    NULL  CM    NULL
2013-01-17 CTF    NR    NULL  NULL  NULL


you can modify that by adding the where clause for course column
 
Share this answer
 
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900