首页 > 试题广场 >

牛客直播开始时各直播间在线人数

[编程题]牛客直播开始时各直播间在线人数
  • 热度指数:31978 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
牛客某页面推出了数据分析系列直播课程介绍。用户可以选择报名任意一场或多场直播课。
已知课程表course_tb如下(其中course_id代表课程编号,course_name表示课程名称,course_datetime代表上课时间):
course_id course_name course_datetime
1 Python 2021-12-1 19:00-21:00
2 SQL 2021-12-2 19:00-21:00
3 R 2021-12-3 19:00-21:00
上课情况表attend_tb如下(其中user_id表示用户编号、course_id代表课程编号、in_datetime表示进入直播间的时间、out_datetime表示离开直播间的时间):
user_id course_id in_datetime out_datetime
100 1 2021-12-01 19:00:00
2021-12-01 19:28:00
100 1 2021-12-01 19:30:00
2021-12-01 19:53:00
101 1 2021-12-01 19:00:00
2021-12-01 20:55:00
102 1 2021-12-01 19:00:00
2021-12-01 19:05:00
104 1 2021-12-01 19:00:00
2021-12-01 20:59:00
101 2 2021-12-02 19:05:00
2021-12-02 20:58:00
102 2 2021-12-02 18:55:00
2021-12-02 21:00:00
104 2 2021-12-02 18:57:00
2021-12-02 20:56:00
107 2 2021-12-02 19:10:00
2021-12-02 19:18:00
100 3 2021-12-03 19:01:00
2021-12-03 21:00:00
102 3 2021-12-03 18:58:00
2021-12-03 19:05:00
108 3 2021-12-03 19:01:00
2021-12-03 19:56:00
请你统计直播开始时(19:00),各科目的在线人数,以上例子的输出结果为(按照course_id升序排序):
course_id course_name online_num
1 Python 4
2
SQL 2
3 R 1
示例1

输入

CREATE TABLE course_tb(
course_id int(10) NOT NULL, 
course_name char(10) NOT NULL,
course_datetime char(30) NOT NULL);
INSERT INTO course_tb VALUES(1, 'Python', '2021-12-1 19:00-21:00');
INSERT INTO course_tb VALUES(2, 'SQL', '2021-12-2 19:00-21:00');
INSERT INTO course_tb VALUES(3, 'R', '2021-12-3 19:00-21:00');

CREATE TABLE attend_tb(
user_id int(10) NOT NULL, 
course_id int(10) NOT NULL,
in_datetime datetime NOT NULL,
out_datetime datetime NOT NULL
);
INSERT INTO attend_tb VALUES(100, 1, '2021-12-1 19:00:00', '2021-12-1 19:28:00');
INSERT INTO attend_tb VALUES(100, 1, '2021-12-1 19:30:00', '2021-12-1 19:53:00');
INSERT INTO attend_tb VALUES(101, 1, '2021-12-1 19:00:00', '2021-12-1 20:55:00');
INSERT INTO attend_tb VALUES(102, 1, '2021-12-1 19:00:00', '2021-12-1 19:05:00');
INSERT INTO attend_tb VALUES(104, 1, '2021-12-1 19:00:00', '2021-12-1 20:59:00');
INSERT INTO attend_tb VALUES(101, 2, '2021-12-2 19:05:00', '2021-12-2 20:58:00');
INSERT INTO attend_tb VALUES(102, 2, '2021-12-2 18:55:00', '2021-12-2 21:00:00');
INSERT INTO attend_tb VALUES(104, 2, '2021-12-2 18:57:00', '2021-12-2 20:56:00');
INSERT INTO attend_tb VALUES(107, 2, '2021-12-2 19:10:00', '2021-12-2 19:18:00');
INSERT INTO attend_tb VALUES(100, 3, '2021-12-3 19:01:00', '2021-12-3 21:00:00');
INSERT INTO attend_tb VALUES(102, 3, '2021-12-3 18:58:00', '2021-12-3 19:05:00');
INSERT INTO attend_tb VALUES(108, 3, '2021-12-3 19:01:00', '2021-12-3 19:56:00');

输出

1|Python|4
2|SQL|2
3|R|1
头像 盐咸咸
发表于 2022-04-12 10:19:30
一、知识点总结 记住这个小知识点: 求某一瞬时同时在线人数的判断条件:  ‘瞬时时间’ between ‘进入时间’ AND ‘离开时间’ 二、解题步骤 题目:统计直播开始时(19:00),各科目的在线人数 开播人数:用户的进入时间在19点前 展开全文
头像 阿翟啊
发表于 2021-11-30 19:51:27
select a.course_id, b.course_name, count(distinct user_id) online_num from attend_tb a left join course_tb b on a.course_id = b.course_ 展开全文
头像 求职小张957
发表于 2024-10-16 21:01:26
首先我把代码写复杂了,不用参考我的代码思想:首先分样本标记,采用聚合函数统计各时刻的最大人数,选择与19.00最接近的时刻,挑选最大值作为19.00最大人数错误:1、方法复杂了,该方法仅适合搜寻所有时刻最大人数,如果只搜索某时刻人数,则可直接根据时间BETWEEN来判断2、MAX(num)如果是针对 展开全文
头像 GuaMiao
发表于 2023-08-01 16:23:11
select course_id, course_name, sum(TIME(in_datetime) <= '19:00:00') as online_num from ( select a.course_id, b.course_name, a.user_id, a.in_dat 展开全文
头像 牛客90585398号
发表于 2022-02-27 10:49:54
select ct.course_id,ct.course_name, sum(if (SUBSTRING(in_datetime,12,5)<=SUBSTRING(course_datetime,11,5),1,0)) from attend_tb as at inner join cour 展开全文
头像 人类1379
发表于 2023-10-31 11:19:02
select a1.course_id, a2.course_name, sum(if(in_datetime <= substr(course_datetime, 1, 15) and out_datetime > substr(course_datetime, 展开全文
头像 凡哥是个小菜鸡
发表于 2022-06-24 15:53:34
select c.course_id,course_name,count(distinct user_id) as online_num from course_tb c inner join attend_tb a using(course_id) where date_format(in_dat 展开全文
头像 牛客931874798号
发表于 2024-10-21 09:59:36
此题核心难点:如何统计 《直播开始时(19:00),各科目的在线人数》 = 各个用户的 in_datetime 登陆时间在19:00或者之前,即统计其在线人数。 select t1.course_id, t1.course_name, sum(case when date_format(t2.in_ 展开全文
头像 牛客786408323号
发表于 2022-07-20 09:47:53
select     course_id,     course_name,     sum(case when time(in_datetime)<'1 展开全文
头像 M.201905171615875
发表于 2022-08-13 15:42:26
select     course_id,     course_name,     count(distinct user_id) as onlin 展开全文

问题信息

难度:
153条回答 1412浏览

热门推荐

通过挑战的用户

查看代码
牛客直播开始时各直播间在线人数