题解|27. 7月份各时间段的登录情况

7月份各时间段的登录情况

明确题意:

统计7月份通勤(7:00-9:00、18:00-20:00),午休(11:00-13:00),临睡(22:00-1:00)时段登录用户数分别是多少

问题拆解:

  • 筛选2021年7月的用户登录记录。知识点:where
  • 判断每条登录记录是否在通勤、午休、临睡时段。知识点:if(.., 1, null);between and
  • 统计每个时段登录的次数。知识点:count()。(如果是统计登录用户数,则需要和user_id一起做distinct)

代码实现:

select
    count(if(login_time between "7:0:0" and "9:0:0" or
       login_time between "18:0:0" and "20:0:0", 1, null)) as `通勤`,
    count(if(login_time between "11:0:0" and "13:0:0", 1, null)) as `午休`,
    count(if(login_time between "22:0:0" and "23:59:59" or
       login_time between "0:0:0" and "1:0:0", 1, null)) as `临睡`
from login_tb
where login_date between "2021-07-01" and "2021-07-31"
全部评论

相关推荐

11-27 14:21
同济大学 Java
卢来猴祖:给了这薪资关键拿不了几个月就给你踹了呀
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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