题解|14. 抖音直播间各个时间段的在线人数

抖音直播间各个时间段的在线人数

明确题意:

统计7:00-9:00(早通勤)、18:00-20:00(晚通勤)、12:00-14:00(午休)、22:00-24:00(临睡)各个时间段的在线人数。

问题拆解:

  • 标记每条记录是否在4个时段内在线。知识点:if(in_time <= "23:59:59" and out_time > "22:00:00", user_id, null)
  • 统计每个时段在线用户数。count(distinct)

代码实现:

select count(distinct if(in_time < "09:00:00" and out_time > "07:00:00", user_id, null)) as `早通勤`,
    count(distinct if(in_time < "20:00:00" and out_time > "18:00:00", user_id, null)) as `晚通勤`,
    count(distinct if(in_time < "14:00:00" and out_time > "12:00:00", user_id, null)) as `午休`,
    count(distinct if(in_time <= "23:59:59" and out_time > "22:00:00", user_id, null)) as `临睡`
from user_view_tb
全部评论

相关推荐

缒梦&独舞:这家公司是这样的,去年给我实习offer了,不过也是面着玩儿的,他周六还要去做公益志愿活动
点赞 评论 收藏
分享
收到了小米的实习offer,犹豫是否要去。。。
认真搞学习:雷总还当过首富呢,公司不算大厂算独角兽吗
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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