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

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

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

with temp as(
  select
    user_id,
    attend_tb.course_id,
    course_name,
    in_datetime as dt,
    1 as flag
  from
    attend_tb
    left join course_tb on attend_tb.course_id = course_tb.course_id
  union all
  select
    user_id,
    attend_tb.course_id,
    course_name,
    out_datetime as dt,
    -1 as flag
  from
    attend_tb
    left join course_tb on attend_tb.course_id = course_tb.course_id
)
select
course_id,course_name,max(num)
from
  (
    select
      course_id,
      course_name,
      sum(flag) over(
        partition by course_id
        order by
          dt
      ) as num
    from
      temp
  ) t
  group by course_id,course_name
  order by course_id
全部评论

相关推荐

投递完美世界等公司6个岗位 >
点赞 评论 收藏
转发
1 收藏 评论
分享
牛客网
牛客企业服务