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

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

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

select
    course_id,
    course_name,
    max(cnt)as max_num
from(select
    course_id,
    course_name,
    sum(flag) over(partition by course_id order by event_time) as cnt
from(select
    at.course_id,
    ct.course_name,
    date_format(ct.course_datetime, '%Y-%m-%d %H:%i:00') as start_time,
    date_format(concat(date_format(ct.course_datetime, '%Y-%m-%d'), ' ', substring_index(substring_index(ct.course_datetime, ' ', -1), '-', -1)), '%Y-%m-%d %H:%i:00') as end_time,
    in_datetime as event_time,
    1 as flag
from attend_tb at
left join course_tb ct
on at.course_id = ct.course_id
union all
select
    at.course_id,
    ct.course_name,
    date_format(ct.course_datetime, '%Y-%m-%d %H:%i:00') as start_time,
    date_format(concat(date_format(ct.course_datetime, '%Y-%m-%d'), ' ', substring_index(substring_index(ct.course_datetime, ' ', -1), '-', -1)), '%Y-%m-%d %H:%i:00') as end_time,
    out_datetime as event_time,
    -1 as flag
from attend_tb at
left join course_tb ct
on at.course_id = ct.course_id)t1
)t2
group by course_id, course_name

全部评论

相关推荐

程序员小屁:帮你了查看图片
点赞 评论 收藏
分享
再懒也要睡懒觉:大学4年玩的挺爽的哈😅
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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