Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 总和和计数不';不能在复杂的SQL查询中工作_Mysql_Count_Sum_Percentage - Fatal编程技术网

Mysql 总和和计数不';不能在复杂的SQL查询中工作

Mysql 总和和计数不';不能在复杂的SQL查询中工作,mysql,count,sum,percentage,Mysql,Count,Sum,Percentage,我必须创建一个带有count、sum的报告,但值不好(红色) SQL FIDDLE: 我的问题是: 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,

我必须创建一个带有count、sum的报告,但值不好(红色)

SQL FIDDLE:

我的问题是:

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;
请参阅。
结果:


请提供表结构和示例数据,以便有人可以帮助您抱歉,我在sqlfiddle链接中创建了完整的模式
| 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%      |