题解 | #牛客直播各科目同时在线人数#
牛客直播各科目同时在线人数
https://www.nowcoder.com/practice/d69677e41f9a4bf3b3ed7a42573e9490
select
t1.course_id,
t2.course_name,
max(t1.num) max_num
from
(
select
course_id,
sum(tag) over(
partition by course_id
order by
time
) num
from
(
select
course_id,
in_datetime time,
1 tag
from
attend_tb
union all
select
course_id,
out_datetime time,
-1 tag
from
attend_tb
) t
) t1
left join course_tb t2 on t1.course_id = t2.course_id
group by
t1.course_id,
t2.course_name
order by
t1.course_id
#sql#
t1.course_id,
t2.course_name,
max(t1.num) max_num
from
(
select
course_id,
sum(tag) over(
partition by course_id
order by
time
) num
from
(
select
course_id,
in_datetime time,
1 tag
from
attend_tb
union all
select
course_id,
out_datetime time,
-1 tag
from
attend_tb
) t
) t1
left join course_tb t2 on t1.course_id = t2.course_id
group by
t1.course_id,
t2.course_name
order by
t1.course_id
#sql#