I had the following physical table of “survey application” result. It’s not relation sort of speak the way storing the result.
As you can see the [84273x1x1] and [84273x1x2] are dropdownlist question which returning code while [84273x1x4] is a free text.
UserID; UserName; Email; [84273x1x1]; [84273x1x2]; [84273x1x4]; [84273x2x5]; [84273x2x6]; [84273x2x7];
1; "Name1"; "name1@email.com"; "A101", "A203", "Test answer bla bla"; "A102", "A201", "Test answer bla bla"
2; "Name2"; "name2@email.com"; "A102", "A202", "This is my comment"; "A101", "A203", "This is my comment";
Something that I found that:
[84273x1x1] is corresponding to:
84273 = SurveyID
1 = PageID
1 = QuestionID
On the Answer table, it has the following:
QID; Code; Answer;
1; A101; 1
1; A102; 2
1; A103; 3
2; A200; 0
2; A201; 1
2; A202; 2
2; A203; 3
5; A101; 1
5; A102; 2
5; A103; 3
6; A200; 0
6; A201; 1
6; A202; 2
6; A203; 3
On the question table:
QID; QuestionType; Title;
1; "DropDownList"; "How do you rate of GROUP-Q1";
2; "DropDownList"; "How do you rate of GROUP-Q1";
3; "Text"; "Comment of Q1";
4; "DropDownList"; "How do you rate of GROUP-Q2";
5; "DropDownList"; "How do you rate of GROUP-Q2";
6; "Text"; "Comment of GROUP-Q2";
The result that I would like to achieve is that pivoting:
UserID; Name; Email; Title; [Question1], [Question2]; [Question3]
1; "Name1"; "name1@email.com"; "GROUP-Q1"; "1"; "3"; "Test answer bla bla";
1; "Name1"; "name1@email.com"; "GROUP-Q2"; "2"; "1"; "Test answer bla bla";
2; "Name2"; "name2@email.com"; "GROUP-Q1"; "2"; "2"; "Test answer bla bla";
2; "Name2"; "name2@email.com"; "GROUP-Q2"; "1"; "3"; "Test answer bla bla";
Because this thing needs to be done in TSQL – 2005. When I looked at this, my first thought will be it has to be in Cursor.
Any thought guys?
Thanks
How about something like so:
Here’s another “more” dynamic solution (requires SQL Server 2005+):
Keep in mind that inherently the SQL language is not designed to deal with dynamic schema (i.e. dynamically generated columns). The only way to build dynamic schema is using dynamic SQL and if you hit that point you might as well do it in the middle tier or a reporting tool. Also, the denormalized structure really makes analysis difficult.