SUM and COUNT doesn't work in complex SQL QUERY

logobi

I have to create a reporting with count, sum but values are not good (red)

enter image description here

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

Here my query :

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;
forpas

The problem with your code is that you are not counting the distinct students for each case.
Try this:

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;

See the demo.
Results:

| 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%      |

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related