Click here to Skip to main content
12,455,917 members (79,784 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: SQL
My Code as follows;
select * FROM Tb_SCh_TIme_Table P PIVOT (MAX(Faculty_Code) FOR Session IN ([1],[2],[3] ,[4])) AS PVT  where course = 'PH2' order by course,Date;
when i run the above query output as follows;
Date       Course   1    2   3   4
15/1/2013   PH2                  NR
15/1/2013   PH2                  CM
select course,  stuff((select ',' + cast(SubQry.Faculty_Code as varchar(50)) from Tb_SCh_TIme_Table As SubQry where SubQry.course = MainQry.course for XML Path('')), 1, 1,'') as Faculty_Code
from Tb_SCh_TIme_Table as MainQry where Course='PH2'
Group by MainQry.course.
when i run the above query output as follows;
Course   Faculty_Code
 PH2      NR,CM
using both query make into single sub query i want the final output to be as follows
 Date       Course   1    2   3    4
15/1/2013   PH2                   NR,CM
how can i do to get the final output as i mentioned above.please help me. i want the answer.
Posted 22-Jan-13 20:58pm
Updated 22-Jan-13 21:43pm
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

Instead of using Tb_SCh_TIme_Table as your source for the pivot use your sub query so your source should have 1 record for each date,course and (1-4)

Date          Course         Num       Value
15/1/2013     PH2            1         NR,CM
15/1/2013     PH2            2         NR
15/1/2013     PH2            3         NR,CM,Oth


When you have that result pivot on the Num column.
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

According to me your following query is wrong.
select course,  stuff((select ',' + cast(SubQry.Faculty_Code as varchar(50)) from Tb_SCh_TIme_Table As SubQry where SubQry.course = MainQry.course for XML Path('')), 1, 1,'') as Faculty_Code
from Tb_SCh_TIme_Table as MainQry where Course='PH2'
Group by MainQry.course.
It should be corrected as follows
SELECT [Date],[Course],[Session],
    SUBSTRING((SELECT ','+ [Faculty_Code]
        FROM [Tb_SCh_TIme_Table ] dp2 
        WHERE dp2.[Date] = dp1.[Date] and dp2.[Course]=dp1.[Course] and dp2.[Session]=dp1.[Session]
    FOR XML PATH('')), 2, 8000) AS [Faculty_Code]
FROM [Tb_SCh_TIme_Table ] dp1
GROUP BY [Date],[Session],[Course]
Here is the complete query according to me
SELECT *
FROM (
SELECT [Date],[Course],[Session],
    SUBSTRING((SELECT ','+ [Faculty_Code]
        FROM [Tb_SCh_TIme_Table ] dp2 
        WHERE dp2.[Date] = dp1.[Date] and dp2.[Course]=dp1.[Course] and dp2.[Session]=dp1.[Session]
    FOR XML PATH('')), 2, 8000) AS [Faculty_Code]
FROM [Tb_SCh_TIme_Table ] dp1
GROUP BY [Date],[Session],[Course]
) P
PIVOT (
MAX([Faculty_Code])
FOR [Session] IN ([1],[2],[3] ,[4])
) AS PVT
WHERE [Course]='PH2'

Please check and get back to me if you need any more details.
  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160826.1 | Last Updated 23 Jan 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100