with a as (select x.course_id,course_name,in_datetime as dt,1 as nb from attend_tb x join course_tb y using(course_id) union all select x.course_id,course_name,out_datetime as dt,-1 as nb from attend_tb x join course_tb y using(course_id) order by 3,4), b as (select a.course_id,a.course_name,sum(a.n...