作为我们正在进行的调查问卷设计的一部分,下面的代码用于确定调查的问题总数、正确答案的数量和正确答案的百分比。
我遇到的一个问题是如何计算复选框列表值。
调查以单选按钮和复选框列表值的形式出现。
使用单选按钮选项时,仅选择 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
我只是不知道如何进行编码。
下面的代码几乎完成了上面列出的所有内容,除了 checkboxlist 值。
提前非常感谢。
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