我有“调查申请”结果的以下物理表。这与存储结果的方式无关。
正如您所见,[84273x1x1]和[84273x1x2]是下拉列表问题,当[84273x1x4]是自由文本时返回代码。
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";
我发现的东西: [84273x1x1]对应于:
84273 = SurveyID
1 = PageID
1 = QuestionID
在答案表上,它具有以下内容:
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";
我想要实现的结果是旋转:
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";
因为这件事需要在TSQL - 2005中完成。当我看到这个时,我首先想到的是它必须在Cursor中。
有谁想过?
谢谢
答案 0 :(得分:1)
如此:
Select P.UserId, P.Username, P.Email
, 'GROUP-Q1'
, A1.Answer As Question1
, A2.Answer As Question2
, P.[84273x1x4] As Question3
From People As P
Left Join Answers As A1
On A1.Code = P.[84273x1x1]
And A1.QID = 1
Left Join Answers As A2
On A2.Code = P.[84273x1x2]
And A2.QID = 2
Union All
Select P.UserId, P.Username, P.Email
, 'GROUP-Q2'
, A1.Answer As Question1
, A2.Answer As Question2
, P.[84273x1x7] As Question3
From People As P
Left Join Answers As A1
On A1.Code = P.[84273x1x5]
And A1.QID = 5
Left Join Answers As A2
On A2.Code = P.[84273x1x6]
And A2.QID = 6
这是另一个“更多”动态解决方案(需要SQL Server 2005+):
;With UserRawAnswers As
(
Select UserId, 1 As QuestionID, [84273x1x1] As Answer From People
Union All Select UserId, 2, [84273x1x2] From People
Union All Select UserId, 3, [84273x1x4] From People
Union All Select UserId, 5, [84273x1x5] From People
Union All Select UserId, 6, [84273x1x6] From People
Union All Select UserId, 6, [84273x1x7] From People
)
, UserAnswers As
(
Select UA.UserId
, Right(Q.Title) As Title
, Coalesce(DropListAnswers.Answer, UA.Answer) As Answer
From UserRawAnswers As UA
Join Questions
On Questions.QID = UA.QID
Left Join (Answer As DropListAnswers
Join Questions As DropListQuestions
On DropListQuestions.QID = DropListAnswers.QID
And DropListQuestions.QuestionType = 'DropDownList')
On DropListAnswers.Code = UA.Answer
)
Select P.UserID, P.Name, P.Email
, UA.Title
, Min( Case When UA.QuestionID = 1 Then UA.Answer End ) As Question1
, Min( Case When UA.QuestionID = 2 Then UA.Answer End ) As Question2
, Min( Case When UA.QuestionID = 3 Then UA.Answer End ) As Question3
From UserAnswers As UA
Join People As P
On P.UserID = UA.UserId
Group By P.UserID, P.Name, P.Email, UA.Title
请记住,SQL语言本身并不是为处理动态模式(即动态生成的列)而设计的。构建动态模式的唯一方法是使用动态SQL,如果达到这一点,您可以在中间层或报告工具中执行此操作。而且,非规范化结构确实使分析变得困难。