Source table:
Create Table ExamAnswers
{
StudentID varchar(12),
QuestionID int,
Answer char(1)
}
and this will be filled with
Bob 1 a
Bob 2 c
...
Bob 100 b
Chris 1 c
Chris 2 d
...
Chris 100 null
etc, for about 500 students.
Chris did not finish the exam, but the 100th question is stored as null, so it is guaranteed that each student has exactly 100 rows, but the actual answer is null or character.
If it makes any difference, answers are in {a,b,c,d,e,f}
This setup works great for the actual exam application, and marking it is trivial.
Now I have a reporting requirement, that for audit purposes, I need to produce a table that looks like this:
ID 1 2 ... 100
Bob a c ... b
Chris c d ....null
So I spent half a day reading about the PIVOT function, and I just don't get it.
That has to be the most impenetrable documentation I've ever read.
For one thing, it requires and aggregate function -- What the heck am I supposed to be aggregating here?
I figure that this is just about the simplest use of the PIVOT function that there could be, and I can't find a decent example anywhere. HELP!