Sql 将两个查询分开以获得每个学期的通过率
我试图为一个表编写一个查询,在这个表中,我希望确定一个学期的通过率是多少 第一个查询(查询1)将给出一个学期通过的学生人数,第二个查询(查询2)将给出一个特定科目的一学期学生总数,代码=4897 我需要执行计算(查询1/查询2)并按学期代码显示结果 问题1:Sql 将两个查询分开以获得每个学期的通过率,sql,Sql,我试图为一个表编写一个查询,在这个表中,我希望确定一个学期的通过率是多少 第一个查询(查询1)将给出一个学期通过的学生人数,第二个查询(查询2)将给出一个特定科目的一学期学生总数,代码=4897 我需要执行计算(查询1/查询2)并按学期代码显示结果 问题1: select count(c.student) as q1, s.semester from course_enrolments c join courses s on c.course = s.id where s.subject
select count(c.student) as q1, s.semester
from course_enrolments c join courses s on c.course = s.id
where s.subject = 4897 and mark >= 50
group by s.semester;
问题2:
select count(c.student) as q2, s.semester
from course_enrolments c join courses s on c.course = s.id
where s.subject = 4897
group by s.semester;
然后是第一季度/第二季度=
结果应该是这样的:
semester | pass_rate |
------+--------------+
03 | 1.00 |
04 | 1.00 |
05 | 1.00 |
06 | 1.00 |
07 | 1.00 |
08 | 0.81 |
09 | 0.89 |
使用条件聚合:
select s.semester,
count(*) as num_students,
sum(case when mark >= 50 then 1 else 0 end) as passes,
avg(case when mark >= 50 then 1.0 else 0 end) as pass_rate,
from course_enrolments c join
courses s
on c.course = s.id
where s.subject = 4897 and mark >= 50
group by s.semester;
使用条件聚合:
select s.semester,
count(*) as num_students,
sum(case when mark >= 50 then 1 else 0 end) as passes,
avg(case when mark >= 50 then 1.0 else 0 end) as pass_rate,
from course_enrolments c join
courses s
on c.course = s.id
where s.subject = 4897 and mark >= 50
group by s.semester;
我删除了不兼容的数据库标记。请仅使用您正在使用的数据库进行标记。我已删除不兼容的数据库标记。请仅使用您正在使用的数据库进行标记。现在就完美了!谢谢好极了!谢谢