子查询找出每门课程总报名人数简单易懂

牛客直播各科目出勤率

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

全部评论

相关推荐

比亚迪汽车新技术研究院 硬件工程师 总包21左右 硕士
点赞 评论 收藏
分享
Natrium_:这时间我以为飞机票
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务