SUM и COUNT не работают в сложном SQL-ЗАПРОСЕ

#mysql #count #sum #percentage

#mysql #подсчет #сумма #процент

Вопрос:

Я должен создать отчет с count, sum, но значения не являются хорошими (красный)

введите описание изображения здесь

СКРИПКА SQL: 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;
 

Комментарии:

1. Пожалуйста, предоставьте структуру таблиц и примеры данных, чтобы кто-нибудь мог вам помочь

2. Извините, я создаю полную схему в sqlfiddle link

Ответ №1:

Проблема с вашим кодом заключается в том, что вы не считаете отдельных учеников для каждого случая.
Попробуйте это:

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