2010-11-10 77 views
0

我有以下「調查申請」結果的物理表。這不是說存儲結果的方式。有沒有想過這個TSQL挑戰?

正如你可以看到[84273x1x1]和[84273x1x2]是下拉列表問題,其中[84273x1x4]是一個自由文本,返回代碼。這

UserID; UserName; Email; [84273x1x1]; [84273x1x2]; [84273x1x4]; [84273x2x5]; [84273x2x6]; [84273x2x7]; 
1; "Name1"; "[email protected]"; "A101", "A203", "Test answer bla bla"; "A102", "A201", "Test answer bla bla" 
2; "Name2"; "[email protected]"; "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"; "[email protected]"; "GROUP-Q1"; "1"; "3"; "Test answer bla bla"; 
1; "Name1"; "[email protected]"; "GROUP-Q2"; "2"; "1"; "Test answer bla bla"; 
2; "Name2"; "[email protected]"; "GROUP-Q1"; "2"; "2"; "Test answer bla bla"; 
2; "Name2"; "[email protected]"; "GROUP-Q2"; "1"; "3"; "Test answer bla bla"; 

因爲這個東西需要在TSQL-2005中完成。當我看到這個時,我的第一個想法就是它必須在Cursor中。

有沒有想過傢伙?

感謝

+0

有多少個問題字段?這個數字是否有望增長? (我猜是的)。這種結構很難處理,最好的做法是將動態數據(問題列)轉換爲新表格,但我也理解這可能是不可能的。我不羨慕必須在這個結構上工作! – JonVD 2010-11-10 23:40:26

回答

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,如果您達到這一點,則不妨在中間層或報告工具中執行此操作。而且,非規範化結構確實使分析困難。