Click here to Skip to main content
15,905,967 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
SQL
select Date,Session,faculty_code,course from Tb_SCh_TIme_Table WHERE COURSE= 'PH2' order by date,Session,course;

output as follows;
Date       session     faculty_code    course
14/1/2013    1            NR             PH2
14/1/2013    4            NR             PH2

But i want the output as follows in sql server 2005;
              PH2 (course)
14/1/2013  1   2   3   4 (Session)
          NR           NR

how can i do? for the above output how to write the query in sql server 2005.
Posted
Updated 19-Jan-13 7:44am
v2

1 solution

you can use PIVOT clause for this.
http://msdn.microsoft.com/en-us/library/ms177410(v=sql.105).aspx[^]

for ex:
SQL
SELECT *
FROM (
SELECT *
FROM [Tb_SCh_TIme_Table ]
) P
PIVOT (
MAX([faculty_code])
FOR [session] IN ([1],[2],[3] ,[4])
) AS PVT
WHERE course='PH2'


above code will give you the output as below
date       course 1     2     3     4
---------- ------ ----- ----- ----- -----
2013-01-14 PH2    NR    NULL  CM    NR
 
Share this answer
 
Comments
nika2008 19-Jan-13 17:03pm    
my 5

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