SELECT DISTINCT SubjectID INTO #Subject FROM tblSetMarks ORDER BY SubjectID
DECLARE @cols NVARCHAR(4000)
SELECT @cols= ISNULL(@cols + ',','') + QUOTENAME(SubjectID) FROM (SELECT DISTINCT SubjectID FROM tblSetMarks ) AS Course
DECLARE @qry NVARCHAR(4000)
SET @qry =
N'SELECT *
FROM (
SELECT t.AdmissionNo,t.ReferenceNo,t.SName,t.FName,t2.Marks,t2.[SubjectID]
FROM tblStdReg t
FULL OUTER JOIN tblSetMarks t2 inner join tblDefSubject as ds on t2.SubjectID=ds.SubjectID
ON t.AdmissionNo = t2.AdmissionNo
) p
PIVOT(Max(Marks) FOR [SubjectID] IN (' +@cols+ ') where ClassID=15) AS pvt '
EXEC(@qry)
DROP TABLE #Subject
This display data of all students. I want to display just Class one student data so how to use where clause in this pivot query. table structure is
Student : StuID, SName,FName
1 abc sky
Marks : StuID,Class,Subject,Marks
1 One Eng 26
2 One Urdu 20