如何使此查询生成我们需要的结果?

时间:2013-08-05 15:06:01

标签: sql sql-server sql-server-2008

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

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)

这就是我的需要。

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

用户可以检查多个选择答案(复选框)

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

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 

问题似乎是正确的。

正确答案并不完全正确,因为如果汤姆克鲁斯电影的情况不止一个选择是正确的,那么所有这些选择都不会显示。

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

最后,我无法找到显示用户选择的方法。

非常感谢您的专业协助。

enter image description here

2 个答案:

答案 0 :(得分: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'

demo

答案 1 :(得分: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