Find Correctly Answered Questions in an Online Test with Single and Multi Choice Questions

南笙酒味 提交于 2019-12-11 07:28:49

问题


See the tables : http://dl.dropbox.com/u/10356431/Shared/screen.png

Please help me to construct a SQL to find the correctly answered questions in an online test for a particular test_id.

I have constructed one.

SELECT COUNT(UNIQUE d.question_id) AS CORRECT
FROM test_response d,
     question_response r
WHERE d.response_id   = r.question_resp_id
AND r.correct_response_flag != 'N'
AND d.test_id = '10113'

But the problem is while it will find the single choice questions accurately, it won't if its a multi-choice as suppose out of 4 responses 2 are correct, choosing one will count it as a correctly answered question which is inaccurate.

Logic: A question set is generated and show to the user. Each test has its own id using a particular question set. The responses chosen by the user are stored in the test_response table.


回答1:


UPDATE: this does not work for OP's table design where 2 rows are created for 4-answer questions

i think you need to first check each question if it has all answers correct and then count the questions without incorrect answers:

select
  count(*) - count(incorrect_answers_per_question) correct
from (
  select
    d.test_id,
    d.question_id,
    sum(case when r.correct_response_flag = 'N' then 1 end) incorrect_answers_per_question
  from test_response d
  join question_response r on d.response_id = r.question_resp_id
  where d.test_id = '10113'
  group by d.test_id, d.question_id
)


来源:https://stackoverflow.com/questions/10275826/find-correctly-answered-questions-in-an-online-test-with-single-and-multi-choice

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!