1

我有以下表格和相关属性:

Survey:
  SurveyID int,
  SurveyTitle nvarchar(50)

SurveyQuestions
 QuestionId int,
 SurveyId fk from Survey table
 Question nvarchar(100)
 QuestionOrder int (eg: 1,2,3)
AnswerType char(1) (M. for Multiple choice answers like Checkbox, S for single like radiobuttion, T for Free Text


SurveyChoices
 ChoiceId int
 QuestionId int fk from SurveyQuestions
 Choice nvarchar(255) (eg: Choices for each question entered by Admin
 IsCorrect bit (1 for Correct choice, 0 for incorrect choice.

SurveyAnswers
 AnswerId int
 ChoiceId int fk from SurveyChoices
 QuetionId int fk from SurveyQuestions
 ChoiceText nvarchar(2000) (For FreeText)

这就是我的需要。

我们想为每个调查显示一个问题,然后是用户的选择,最后是正确的答案或答案。

用户可以选中 1 个以上的多项选择答案(复选框)

所以,布局应该是这样的:

Question                               Your Choice                     Correct Answer(s)
How many days in a year?                 365                            365
Tom Cruise Starred in 
what movies? Check all that apply       Rain Man                        Rain Man
                                        Risky Business                  Risky Business
                                        Mission Possible                Mission Impossible

您实际上可以在小提琴上看到 sql 架构

http://sqlfiddle.com/#!3/4c642/1

礼貌史蒂夫福特。再次感谢史蒂夫。

我尝试的查询如下:

SELECT DISTINCT SQ.question, SC.Choice As CorrectAnswer   
                          FROM surveyquestions AS SQ
                          INNER JOIN surveychoices AS SC ON SQ.QuestionID = SC.QuestionID
                          INNER JOIN surveyanswers AS SA ON SA.QuestionID = SQ.QuestionID AND SC.choiceId = SA.choiceId AND SA.username = 'John.Doe'
                          WHERE SQ.SurveyID = 10 

这些问题似乎是正确的。

正确的答案并不完全正确,因为如果像汤姆克鲁斯电影那样有多个选项是正确的,那么所有选项都不会显示出来。

我们希望显示每个问题的所有正确答案。

最后,我想不出一种显示用户选择的方法。

非常感谢您的专家协助。

在此处输入图像描述

4

2 回答 2

1

我认为基本上你想要GROUP_CONCAT正确的选择和给定的答案分别然后加入问题

像这样的东西

WITH CorrectChoices AS
(
  SELECT QuestionID, LEFT(CorrectChoice , LEN(CorrectChoice )-1) AS Choice
  FROM SurveyChoices AS extern
  CROSS APPLY
  (
    SELECT Choice + ', '
    FROM SurveyChoices AS intern
    WHERE extern.QuestionID = intern.QuestionID
    AND intern.IsCorrect=1
    ORDER BY Choice
    FOR XML PATH('')
  ) pre_trimmed (CorrectChoice)
  GROUP BY QuestionID, CorrectChoice
)
, Answer AS
(
  SELECT QuestionID, UserName, LEFT(CorrectChoice , LEN(CorrectChoice )-1) AS Choice
  FROM SurveyAnswers AS extern
  CROSS APPLY
  (
    SELECT Choice + ', '
    FROM SurveyAnswers intern
    INNER JOIN SurveyChoices SC ON SC.QuestionId = intern.QuestionID
                               AND SC.ChoiceID = intern.ChoiceID
    WHERE extern.QuestionID = intern.QuestionID
    ORDER BY Choice
    FOR XML PATH('')
  ) pre_trimmed (CorrectChoice)
  GROUP BY QuestionID, UserName, CorrectChoice
)

SELECT SQ.Question, CC.Choice, A.Choice Answer, 
       CASE WHEN CC.Choice = A.Choice THEN 'Correct' ELSE 'Wrong' END
FROM SurveyQuestions SQ
LEFT JOIN CorrectChoices CC ON CC.QuestionID = SQ.QuestionID
LEFT JOIN Answer A ON A.QuestionID =SQ.QuestionID
WHERE SQ.SurveyID = 10
AND UserName='John Doe'

演示

于 2013-08-05T15:41:11.593 回答
0
SELECT Q.question, C.choice, 
  (SELECT  A1.choice FROM SurveyChoices A1 WHERE A1.choiceid = A.choiceid) AS YourChoice
FROM SurveyQuestions AS Q
left JOIN 
  (SELECT C1.questionid, C1.Choice from SurveyChoices C1 WHERE C1.iscorrect = 1) AS C
   ON Q.questionid = C.questionid
left  JOIN SurveyAnswers A ON A.questionid = Q.questionid
WHERE Q.surveyid = 10  ORDER BY Q.questionorder;

http://sqlfiddle.com/#!3/4c642/99

于 2013-08-05T15:58:53.817 回答