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

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

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

select t2.course_id,course_name,max(t2.num)
from 
(
select
    t1.course_id,
    sum(flag) over (
        partition by
            t1.course_id
        order by
            dt asc,
            flag desc
    ) as num
from
    (
        select
            course_id,
            in_datetime as dt,
            1 as flag
        from
            attend_tb
        union all
        select
            course_id,
            out_datetime as dt,
            -1 as flag
        from
            attend_tb
    ) as t1
) as t2
left join course_tb
using(course_id)
group by t2.course_id,course_name;

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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