子查询找出每门课程总报名人数简单易懂
牛客直播各科目出勤率
https://www.nowcoder.com/practice/0cab547df4f0430b93042128f445d899
子查询计算每门课程的总人数,(select count(distinct user_id) from behavior_tb as t2 where t2.course_id=t3.course_id and if_sign=1 group by t2.course_id )
出勤人数:count(distinct if(timestampdiff(minute,in_datetime,out_datetime)>=10,t3.user_id,null))
然后只用把表1和表3join就可以了:course_tb as t1 join attend_tb as t3 using(course_id)
select course_id, course_name, round(100*(count(distinct if(timestampdiff(minute,in_datetime,out_datetime)>=10,t3.user_id,null))/(select count(distinct user_id) from behavior_tb as t2 where t2.course_id=t3.course_id and if_sign=1 group by t2.course_id )),2) as 'attend_rate(%)' from course_tb as t1 join attend_tb as t3 using(course_id) group by course_id,course_name order by course_id