基本的SQL查询

时间:2014-11-09 12:04:09

标签: sql oracle

我有以下表格和列

table 1: student answers
columns: question_id, student_answer,student_id,test_id
table 2: questions
columns: question_id,correct answer

我必须列出超过30%的学生错误地测试的问题。 我已经列出了问题以及每个问题的失败率百分比。 我如何只列出超过30%的学生错误回答的问题。

select q.question_id,count(student_id)*100/
(select count(distinct student_id)from student_answers ) 
       as "Percentage Student Fail Rate"
from student_answers s,questions q
where q.question_id=s.question_id 
and
correct_answer<>student_answer
group by q.question_id;

提前致谢

2 个答案:

答案 0 :(得分:1)

我建议您使用子查询,并且不要在列的名称中使用特殊字符。必须转义列名称很尴尬:

select sq.*
from (select q.question_id,
             count(student_id)*100/(select count(distinct student_id)from student_answers ) as PercentageStudentFailRate
      from student_answers s join
           questions q
           on q.question_id=s.question_id 
      correct_answer<>student_answer
      group by q.question_id
     ) sq
where PercentageStudentFailRate > 0.3;

说完了(并修复了查询,使其具有显式连接。我建议你使用条件聚合进行查询:

select sq.*
from (select q.question_id,
             avg(case when correct_answer <> student_answer then 1.0 else 0 end)*100 as PercentageStudentFailRate
      from student_answers s join
           questions q
           on q.question_id=s.question_id 
      correct_answer<>student_answer
      group by q.question_id
     ) sq
where PercentageStudentFailRate > 30.0;

这消除了对子查询的需要。

答案 1 :(得分:0)

您应该可以使用HAVING运算符获得答案:

select q.question_id,count(student_id)*100/
(select count(distinct student_id)from student_answers ) 
       as "Percentage Student Fail Rate"
from student_answers s,questions q
where q.question_id=s.question_id 
    and correct_answer<>student_answer
group by q.question_id
having count(student_id)*100/(select count(distinct student_id)from student_answers ) > 30