SUM和COUNT在复杂的SQL查询中不起作用

徽标

我必须创建一个具有计数,总和但值不佳的报告(红色)

在此处输入图片说明

SQL FIDDLE:http://sqlfiddle.com/#!9 / 9aba2d / 2/0

这是我的查询:

select 
    courses.id_courses,
    courses.name_courses,
    count(DISTINCT list_courses_prof.id_courses_prof) AS nbProfessors,
    count(DISTINCT list_courses_stud.id_courses_stud) AS nbStudents,
    sum(if(list_courses_stud.present_stud > 0,1,0)) AS nbPresents,
    concat(round(sum(if(list_courses_stud.present_stud > 0,1,0)) / count(list_courses_stud.id_courses_stud) * 100,0),'%') AS pctPresent,
    sum(if(list_gender.name_gender = 'Man',1,0)) AS NbMan,
    concat(round(sum(if(list_gender.name_gender = 'Man',1,0)) / count(list_courses_stud.id_courses_stud) * 100,0),'%') AS pctMan,
    sum(if(list_gender.name_gender = 'Woman',1,0)) AS NbWoman,
    concat(round(sum(if(list_gender.name_gender = 'Woman',1,0)) / count(list_courses_stud.id_courses_stud) * 100,0),'%') AS pctWoman 
from courses 
left join list_courses_stud     on courses.id_courses                   = list_courses_stud.id_courses_join
left join list_courses_prof     on courses.id_courses                   = list_courses_prof.id_courses_join
left join students              on list_courses_stud.id_student_join    = students.id_student
left join list_gender           on students.id_gender_join              = list_gender.id_gender
group by courses.id_courses 
order by courses.id_courses desc;
福帕斯

代码的问题在于,您没有为每种情况都计算出不同的学生。
尝试这个:

select c.id_courses, c.name_courses,
       count(DISTINCT cp.id_courses_prof) AS nbProfessors,
       count(DISTINCT s.id_student) AS nbStudents,
       count(DISTINCT CASE WHEN cs.present_stud > 0 THEN s.id_student END) AS nbPresents,
       concat(round(sum(cs.present_stud > 0) / count(cs.id_courses_stud) * 100, 0), '%') AS pctPresent,
       count(DISTINCT CASE WHEN g.name_gender = 'Man' THEN  s.id_student END) AS NbMan,
       concat(round(count(DISTINCT CASE WHEN g.name_gender = 'Man' THEN  s.id_student END) / count(DISTINCT s.id_student) * 100, 0), '%') AS pctMan,
       count(DISTINCT CASE WHEN g.name_gender = 'Woman' THEN  s.id_student END) AS NbWoman,
       concat(round(count(DISTINCT CASE WHEN g.name_gender = 'Woman' THEN  s.id_student END) / count(DISTINCT s.id_student) * 100, 0), '%') AS pctWoman 
from courses c
left join list_courses_stud cs on c.id_courses = cs.id_courses_join
left join list_courses_prof cp on c.id_courses = cp.id_courses_join
left join students s on cs.id_student_join = s.id_student
left join list_gender g on s.id_gender_join = g.id_gender
group by c.id_courses, c.name_courses 
order by c.id_courses desc;

参见演示
结果:

| id_courses | name_courses | nbProfessors | nbStudents | nbPresents | pctPresent | NbMan | pctMan | NbWoman | pctWoman |
| ---------- | ------------ | ------------ | ---------- | ---------- | ---------- | ----- | ------ | ------- | -------- |
| 3          | Technnology  | 3            | 3          | 2          | 67%        | 2     | 67%    | 1       | 33%      |
| 2          | Music        | 2            | 2          | 2          | 100%       | 2     | 100%   | 0       | 0%       |
| 1          | Paint        | 1            | 5          | 4          | 80%        | 3     | 60%    | 2       | 40%      |

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章