1

查看表格:http ://dl.dropbox.com/u/10356431/Shared/screen.png

请帮助我构建一个 SQL 以在特定 test_id 的在线测试中找到正确回答的问题。

我已经构建了一个。

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'

但问题是,虽然它会准确地找到单选题,但如果它是多选题,假设 4 个回答中有 2 个是正确的,则选择一个将被视为正确回答的问题,这是不准确的。

逻辑:生成问题集并显示给用户。每个测试都有自己的 ID,使用特定的问题集。用户选择的响应存储在test_response表中。

4

1 回答 1

0

更新:这不适用于 OP 的表格设计,其中为 4-answer 问题创建了 2 行

我认为您需要首先检查每个问题是否所有答案都正确,然后计算没有错误答案的问题:

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
)
于 2012-04-23T07:25:59.280 回答