tblDateSheet
SetExamID
|
ClassID
|
SectionID
|
SubjectID
|
STime
|
ETime
|
ExamDate
|
14
|
1
|
50
|
10
|
9:00
|
11:00
|
2018.08.24
|
14
|
1
|
50
|
11
|
9:00
|
11:00
|
2018.08.25
|
14
|
1
|
50
|
12
|
9:00
|
11:00
|
2018.08.26
|
15
|
1
|
50
|
10
|
9:00
|
11:00
|
2018.09.27
|
15
|
1
|
50
|
11
|
9:00
|
11:00
|
2018.09.28
|
15
|
1
|
50
|
12
|
9:00
|
11:00
|
2018.09.29
|
I want output like this using Dynamic Pivot Query
I have written this query
DECLARE @cols AS NVARCHAR(MAX),
@query AS NVARCHAR(MAX)
select @cols = STUFF((SELECT ',' + QUOTENAME(ExamDate) from tblDateSheet
group by ExamDate order by ExamDate FOR XML PATH(''), TYPE ).value('.', 'NVARCHAR(MAX)')
,1,1,'')
set @query = 'SELECT SetExamID,ClassID,SectionID,SubjectID,STime,ETime' + @cols + ' from
(
select SetExamID,ClassID,SectionID,SubjectID,STime,ETime,ExamDate
from tblDateSheet ) x
pivot
(
max(SubjectID)
for ExamDate in (' + @cols + ')
) p '
execute(@query);
it is showing the following error Invalid Colomn Name SubjectID