题解 | #牛客直播各科目出勤率#
牛客直播各科目出勤率
http://www.nowcoder.com/practice/0cab547df4f0430b93042128f445d899
这题的难点在于怎么链接,要表现出所有用户,因此behavior表是base表,再左连接,才不会丢失用户信息
select be.course_id,course_name,
round(100*sum(if(sum_time>=10,1,0))
/sum(if(if_sign=1,1,0)),2) as attend_rate
from behavior_tb be left join
(select user_id,course_id,
sum(timestampdiff(minute,in_datetime,out_datetime)) as sum_time
from attend_tb
group by user_id,course_id
) att using(user_id,course_id)
left join course_tb cou on be.course_id=cou.course_id
group by be.course_id,course_name