首页 > 试题广场 >

牛客直播各科目平均观看时长

[编程题]牛客直播各科目平均观看时长
  • 热度指数:20166 时间限制: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
请你统计每个科目的平均观看时长(观看时长定义为离开直播间的时间与进入直播间的时间之差,单位是分钟),输出结果按平均观看时长降序排序,结果保留两位小数。
course_name avg_Len
SQL 91.25
R 60.33
Python 58.00
示例1

输入

drop table if exists course_tb;
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');

drop table if exists attend_tb;
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');

输出

SQL|91.25
R|60.33
Python|58.00
头像 盐咸咸
发表于 2022-04-12 10:34:49
题目比较简单,但是我卡在了:是否要去重用户这件事儿上了,比如一个用户在直播期间重复进入退出好几次,这个咋算?本以为要对用户进行去重,结果发现不用。 SELECT course_name,ROUND(AVG(TIMESTAMPDIFF(MINUTE,in_datetime,out_dat 展开全文
头像 GuaMiao
发表于 2023-08-01 16:34:55
select course_name, round(AVG(duration), 2) as avg_Len from ( select b.course_name, a.course_id, TIMESTAMPDIFF(MINUTE, a.in_datetime, a.out_dateti 展开全文
头像 酸菜鱼土豆大侠
发表于 2022-10-30 18:04:29
【场景】:观看时长 【分类】:分组查询、多表连接 分析思路 难点: 1.计算(观看时长定义为离开直播间的时间与进入直播间的时间之差,单位是分钟),使用timestampdiff() (1)请你统计每个科目的平均观看时长,输出结果按平均观看时长降序排序,结果保留两位小数 [使用]:timest 展开全文
头像 对牛可弹琴
发表于 2022-02-19 21:06:05
```SELECT c.course_name, ROUND( AVG( TIMESTAMPDIFF( MINUTE, in_datetime, out_datetime ) ), 2 ) avg_len FROM attend_tb a LEFT JOIN course_tb c ON 展开全文
头像 牛客283607898号
发表于 2025-02-21 15:06:33
# select course_name, # round(avg(timestampdiff(minute,if(in_datetime<=substring(course_datetime,1,15),course_datetime,in_datetime),out_dat 展开全文
头像 热情的悲伤蛙求求offer
发表于 2025-05-09 16:02:15
with a as( select ct.course_name, round(avg(timestampdiff(second,at.in_datetime,at.out_datetime)/60),2) as avg_Len from 展开全文
头像 找工作好难玉玉了😇
发表于 2024-02-06 11:08:29
# 求每个科目的平均观看时长,单位是分钟 # 先对上课情况表的时长求秒数,再联结两张表,分组求均值 # 求均值,再除以60得到分钟单位 SELECT course_name, ROUND(AVG(time_gap)/60, 2) avg_Len FROM course_tb JOIN ( # 求时 展开全文
头像 飞机炸弹_
发表于 2023-10-08 23:16:50
select course_name, round(avg(timestampdiff(minute, in_datetime, out_datetime)),2) as avg_Len from attend_tb join course_tb using (course_id) group 展开全文
头像 小王要加油哦
发表于 2022-04-10 10:26:01
timestampdiff求时间差的函数 语法:timestampdiff(interval(单位),time_start,time_end) select course_name, round(avg(timestampdiff(minute,in_datetime,out_datetime)), 展开全文
头像 Lovelace_Wan
发表于 2024-03-16 15:19:47
# # 请你统计每个科目的平均观看时长(观看时长定义为离开直播间的时间与进入直播间的时间之差,单位是分钟),输出结果按平均观看时长降序排序,结果保留两位小数。 # [使用]:timestampdiff(minute,in_datetime,out_datetime) select course_n 展开全文

问题信息

难度:
104条回答 980浏览

热门推荐

通过挑战的用户

查看代码
牛客直播各科目平均观看时长