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

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

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

# 作答时间:20240417 14:52 ~ 
# 字段: course_id、course_name、max_num
# 问题类型:最大在线人数
# 方法:①union all  in_time和out_time;②sum(label)over()按照课程分组、time(升)和label(降)排序;③max筛选最大在线人数
# tb1:union all
with tb1 as (
    select user_id,course_id,course_name,in_datetime as inout_time,1 as label
    from attend_tb left join course_tb using(course_id)
    union all 
    select user_id,course_id,course_name,out_datetime as inout_time,-1 as label
    from attend_tb left join course_tb using(course_id)
),
# tb2:累计在线人数统计
tb2 as(
    select course_id,course_name,sum(label) over(partition by course_id order by inout_time,label desc) as sum_uv
    from tb1
)
# 最大在线人数max统计
select course_id,course_name,max(sum_uv)
from tb2
group by course_id,course_name
order by course_id


全部评论

相关推荐

小浪_Coding:1. 个人技能排版太乱, 写的技术栈太浅了, 跟测试,自动化相关的太少; 2. 项目开发类的太简单没有亮点, 算法类的项目建议只放一个,最好有自动化,CI/CD, pipline的项目, 需要更换; 3.整体排版需要优化, SOOB打招呼都需要注意等.
我的简历长这样
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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