如何计算复选框列表值?

时间:2013-07-25 16:33:57

标签: asp.net sql sql-server

作为我们正在进行的调查问卷设计的一部分,以下代码用于确定调查问题的总数,正确数量和正确答案的百分比。

我遇到的一个问题是如何计算复选框列表值。

调查以单选按钮和复选框列表值的形式出现。

选择单选按钮时,只选择1个选项。

但是对于checkboxlist,可能有超过1个值。

这使得计算复选框列表值变得困难。

我的想法是按以下方式计算复选框值:

-- plus 1 for each correct answer
-- minus 1 for each wrong answer
-- total from that then divided by the number of correct answers to the question

我只是不知道如何编码。

以下是除了复选框列表值之外几乎所有上面列出的代码。

提前多多感谢。

SELECT question, choice, CorrectAnswer, TotalAnswers, CorrectAnswers, 
                                (CorrectAnswers * 100) / TotalAnswers AS totalPercent, 
                                convert(varchar, (CorrectAnswers * 100) / TotalAnswers ) + '%' AS totalPercentStr
                                FROM (
                                     SELECT sq.questionid, sq.question, sc.choice, sq.CorrectAnswer, 
                                       COUNT(sq.questionId) OVER (PARTITION BY sq.SurveyId) AS TotalAnswers, 
                                       COUNT(CASE WHEN sa.choiceid IS NOT NULL AND sc.choice = sq.CorrectAnswer THEN 1 ELSE NULL END) OVER (PARTITION BY sq.SurveyId) AS CorrectAnswers
                                     FROM Survey s
                                     INNER JOIN SurveyQuestions AS sq ON s.surveyId = sq.SurveyId
                                     INNER JOIN SurveyChoices   AS sc ON sq.questionId = sc.questionId
                                     LEFT JOIN SurveyAnswers    AS sa ON sc.choiceId = sa.choiceId AND sa.username = @UserName
                                     WHERE s.surveyId = @SurveyId
                                     AND (sa.username IS NOT NULL
                                     OR
                                         (sa.username IS NULL AND sc.choice = sq.CorrectAnswer))
                                    ) AS derived
                                ORDER BY questionId;
CREATE TABLE [dbo].[Survey](
  [SurveyID] [int] IDENTITY(1,1) NOT NULL,
  [Title] [varchar](50) NULL,
  [Description] [varchar](max) NULL

CREATE TABLE [dbo].[SurveyQuestions](
  [QuestionID] [int] IDENTITY(1,1) NOT NULL,
  [SurveyID] [int] NULL,
  [Question] [varchar](255) NULL,
  [AnswerType] [char](1) NULL,
  [CorrectAnswer] [nvarchar](255) NULL,
  [QuestionOrder] [int] NULL


CREATE TABLE [dbo].[SurveyChoices](
  [ChoiceID] [int] IDENTITY(1,1) NOT NULL,
  [QuestionID] [int] NOT NULL,
  [Choice] [nvarchar](255) NOT NULL

CREATE TABLE [dbo].[SurveyAnswers](
  [AnswerID] [int] IDENTITY(1,1) NOT NULL,
  [QuestionID] [int] NOT NULL,
  [ChoiceID] [int] NULL,
  [ChoiceText] [varchar](max) NULL,
  [UserName] [varchar](50) NULL

1 个答案:

答案 0 :(得分:0)

我建议添加如下表格:

CREATE TABLE [dbo].[CorrectAnswers](
  [AnswerID] [int] IDENTITY(1,1) NOT NULL,
  [QuestionID] [int] NOT NULL,
  [ChoiceID] [int] NULL,
  [ChoiceText] [varchar](max) NULL
)

...并预先填充所有正确答案。

当用户点击提交时,表格SurveyAnswers将会填充。我认为已经完成了(否则,SurveyAnswers表的重点是什么)

然后,您可以执行以下操作,以正确的答案并排查看结果:

SELECT 
    sc.QuestionID, 
    sc.ChoiceID, 
    case when sa.choiceID IS NOT NULL then 1 else 0 end as userSelected, 
    case when ca.choiceID IS NOT NULL then 1 else 0 end as isCorrectAnswer 
from dbo.SurveyChoices sc
LEFT JOIN dbo.SurveyAnswers sa on sc.ChoiceID = sa.choiceID AND sc.QuestionID = sa.QuestionID
LEFT JOIN dbo.CorrectAnswers ca on sc.ChoiceID = ca.choiceID AND sc.QuestionID = ca.QuestionID

请注意,如果您的choiceID是唯一的,那么您不需要在连接条件中包含QuestionID。

此外,不确定您是否需要表中的ChoiceText列 - 它应该只在一个表中,我的猜测是该表为SurveyChoices