首页 > 试题广场 >

短视频直播间晚上11-12点之间各直播间的在线人数

[编程题]短视频直播间晚上11-12点之间各直播间的在线人数
  • 热度指数:9754 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
现有某天短视频直播间用户观看直播间的信息表user_view_tb如下,
(其中字段包含用户id:user_id、直播间id:room_id、  进入时间:in_time,离开时间:out_time)
user_id room_id in_time out_time
1 1001 10:00:00 10:30:00
2 1001 10:01:00 10:05:00
3 1001 10:05:00 10:20:00
1 1002 19:05:00 20:05:00
2 1002 19:15:00 19:55:00
2 1002 20:15:00 20:45:00
3 1002 20:15:00 20:45:00
4 1003 22:15:00 23:15:00
1 1002 23:15:00 23:45:00
4 1002 23:10:00 23:25:00
3 1002 23:00:00 23:35:00
4 1001 23:10:00 23:25:00
3 1001 23:00:00 23:35:00
4 1003 23:10:00 23:15:00
1 1001 20:10:00 20:15:00
1 1001 20:00:00 23:35:00
有直播间信息表room_info_tb如下:
room_id room_name room_type
1001 娱乐大王牌 娱乐
1002 声家班 搞笑
1003 嗨嗨嗨 搞笑
请你统计晚上11-12点之间各直播间的在线人数(包含边界值11:00、12:00),并按在线人数降序排序,以上例子输出结果如下:
room_id room_name user_count
1001 娱乐大王牌 3
1002 声家班 3
1003 嗨嗨嗨 1


示例1

输入

drop table if exists user_view_tb;
CREATE TABLE user_view_tb(
user_id int(10) NOT NULL,
room_id int(10) NOT NULL,
in_time time NOT NULL,
out_time time NOT NULL
);
INSERT INTO user_view_tb VALUES(1, 1001, '10:00:00', '10:30:00');
INSERT INTO user_view_tb VALUES(2, 1001, '10:01:00', '10:05:00');
INSERT INTO user_view_tb VALUES(3, 1001, '10:05:00', '10:20:00');
INSERT INTO user_view_tb VALUES(1, 1002, '19:05:00', '20:05:00');
INSERT INTO user_view_tb VALUES(2, 1002, '19:15:00', '19:55:00');
INSERT INTO user_view_tb VALUES(2, 1002, '20:15:00', '20:45:00');
INSERT INTO user_view_tb VALUES(3, 1002, '20:15:00', '20:45:00');
INSERT INTO user_view_tb VALUES(4, 1003, '22:15:00', '23:15:00');
INSERT INTO user_view_tb VALUES(1, 1002, '23:15:00', '23:45:00');
INSERT INTO user_view_tb VALUES(4, 1002, '23:10:00', '23:25:00');
INSERT INTO user_view_tb VALUES(3, 1002, '23:00:00', '23:35:00');
INSERT INTO user_view_tb VALUES(4, 1001, '23:10:00', '23:25:00');
INSERT INTO user_view_tb VALUES(3, 1001, '23:00:00', '23:35:00');
INSERT INTO user_view_tb VALUES(4, 1003, '23:10:00', '23:15:00');
INSERT INTO user_view_tb VALUES(1, 1001, '20:10:00', '20:15:00');
INSERT INTO user_view_tb VALUES(1, 1001, '20:00:00', '23:35:00');

drop table if exists room_info_tb;
CREATE TABLE room_info_tb(
room_id int(10) NOT NULL,
room_name varchar(20) NOT NULL,
room_type varchar(20) NOT NULL
);
INSERT INTO room_info_tb VALUES(1001, '娱乐大王牌', '娱乐');
INSERT INTO room_info_tb VALUES(1002, '声家班', '搞笑');
INSERT INTO room_info_tb VALUES(1003, '嗨嗨嗨', '搞笑');

输出

1001|娱乐大王牌|3
1002|声家班|3
1003|嗨嗨嗨|1
头像 牛客题解官
发表于 2025-02-27 16:08:21
精华题解 这道题目要求我们统计在晚上特定时间段里各个直播间的在线人数,我们要做的事情如下: 1. 确定总体问题 我们需要统计在晚上11点到12点之间,各个直播间的在线人数,并按在线人数降序排序。 2. 分析关键问题 连接表:将user_view_tb和room_info_tb表连接起来,以便获取每个直播间的 展开全文
头像 牛客489527393号
发表于 2025-02-18 18:08:22
select room_id, max(room_name), count(distinct user_id) as user_count from user_view_tb join room_info_tb using(room_id) where out_time>='23:00:00' 展开全文
头像 牛客134017745号
发表于 2025-04-11 15:03:21
select r.room_id, r.room_name, count(distinct u.user_id) as user_count from room_info_tb r left join user_view_tb u on r.room_id = u.room_id where in 展开全文
头像 牛客102435226号
发表于 2025-02-20 11:17:12
select h1.room_id,h2.room_name,h1.user_count from (select room_id,count(distinct user_id) as user_count from user_view_tb where substr(in_time,1,2) in 展开全文
头像 刷牛客的coder很爱吃香菜
发表于 2025-02-13 14:37:21
select uv.room_id, room_name, count(distinct user_id) user_count from user_view_tb uv join room_info_tb rit on uv.room_id = rit.ro 展开全文
头像 木流_牛马
发表于 2025-03-28 07:47:45
with T1 as ( select * from user_view_tb u where (u.in_time between '23:00:00' and '23:59:59') or (u.out_time between '23:00:00' and 展开全文
头像 橙子654
发表于 2025-04-28 23:24:16
with tb as( #2.把不满足if条件的user_id的记录过滤,并对同一直播间多次进入的同一用户用group by去重(人次转为人数) select user_id,room_id from( #1.查找进入和退出直播间时间在23到24点之间的user_id和对应的直播间id SELE 展开全文
头像 谦虚的比尔在炒股
发表于 2025-06-15 12:06:25
select a.room_id, rit.room_name, a.user_count from ( select room_id, count(distinct user_id) as user_count 展开全文
头像 牛客447848010号
发表于 2025-06-19 22:08:40
with a as( select room_id,user_id,date_format(in_time,'%H') as intime,date_format(out_time,'%H') as outtime from user_view_tb having intime in (23,24 展开全文
头像 牛客695261949号
发表于 2025-04-07 10:14:25
select t1.room_id , room_name, user_count from ( select room_id,count(distinct user_id) as user_count from user_view_tb where substr(in_time,1,2)>= 展开全文
头像 白鸟予林
发表于 2025-03-03 18:19:46
select u.room_id, room_name, count(distinct user_id) user_count from user_view_tb u join room_info_tb rap on u.room_id = rap.room_ 展开全文