题解 | #牛客直播各科目同时在线人数#

牛客直播各科目同时在线人数

https://www.nowcoder.com/practice/d69677e41f9a4bf3b3ed7a42573e9490

#同时在线最大人数问题,主要思路为上线设置为1,下线设置为-1,通过开窗函数加总可以得到同时在线人数
with a as (select sum(status)over(partition by a.course_id order by time) as max_num,a.course_id,course_name
from
(select course_id,in_datetime as time,case when in_datetime is not null then 1 end as status
from attend_tb 
union all
select course_id,out_datetime as time,case when out_datetime is not null then -1 end as status 
from attend_tb) a
left join course_tb b
on a.course_id=b.course_id)

select course_id,course_name,max(max_num)
from a 
group by course_id,course_name

全部评论

相关推荐

点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务