问题
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