select t1.course_id, course_name, count(distinct case when t1.course_id=1 and '2021-12-1 19:00' between in_datetime and out_datetime then user_id when t1.course_id=2 and '2021-12-2 19:00' between in_datetime and out_datetime then user_id when t1.course_id=3 and '2021-12-3 19:00' between in_datetime ...