首页 > 试题广场 >

2021年11月每天的人均浏览文章时长

[编程题]2021年11月每天的人均浏览文章时长
  • 热度指数:88724 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
用户行为日志表tb_user_log
id uid artical_id in_time out_time sign_cin
1 101 9001 2021-11-01 10:00:00 2021-11-01 10:00:31 0
2 102 9001
2021-11-01 10:00:00 2021-11-01 10:00:24 0
3 102 9002
2021-11-01 11:00:00 2021-11-01 11:00:11 0
4 101 9001 2021-11-02 10:00:00 2021-11-02 10:00:50 0
5 102 9002
2021-11-02 11:00:01 2021-11-02 11:00:24
0
(uid-用户ID, artical_id-文章ID, in_time-进入时间, out_time-离开时间, sign_in-是否签到)


场景逻辑说明artical_id-文章ID代表用户浏览的文章的ID,artical_id-文章ID0表示用户在非文章内容页(比如App内的列表页、活动页等)。

问题:统计2021年11月每天的人均浏览文章时长(秒数),结果保留1位小数,并按时长由短到长排序。

输出示例
示例数据的输出结果如下

dt avg_viiew_len_sec
2021-11-01 33.0
2021-11-02 36.5

解释:
11月1日有2个人浏览文章,总共浏览时长为31+24+11=66秒,人均浏览33秒;
11月2日有2个人浏览文章,总共时长为50+23=73秒,人均时长为36.5秒。
示例1

输入

DROP TABLE IF EXISTS tb_user_log;
CREATE TABLE tb_user_log (
    id INT PRIMARY KEY AUTO_INCREMENT COMMENT '自增ID',
    uid INT NOT NULL COMMENT '用户ID',
    artical_id INT NOT NULL COMMENT '视频ID',
    in_time datetime COMMENT '进入时间',
    out_time datetime COMMENT '离开时间',
    sign_in TINYINT DEFAULT 0 COMMENT '是否签到'
) CHARACTER SET utf8 COLLATE utf8_bin;

INSERT INTO tb_user_log(uid, artical_id, in_time, out_time, sign_in) VALUES
  (101, 9001, '2021-11-01 10:00:00', '2021-11-01 10:00:31', 0),
  (102, 9001, '2021-11-01 10:00:00', '2021-11-01 10:00:24', 0),
  (102, 9002, '2021-11-01 11:00:00', '2021-11-01 11:00:11', 0),
  (101, 9001, '2021-11-02 10:00:00', '2021-11-02 10:00:50', 0),
  (102, 9002, '2021-11-02 11:00:01', '2021-11-02 11:00:24', 0);

输出

2021-11-01|33.0
2021-11-02|36.5
头像 菜*鸟*1*号
发表于 2021-12-03 10:29:44
select date(in_time) dt, round(sum(timestampdiff(second,in_time,out_time)) / count(distinct uid),1) avg_lensec from tb_user_log where date_for 展开全文
头像 webary
发表于 2021-12-03 22:09:20
2021年11月每天的人均浏览文章时长 明确题意: 统计2021年11月每天的人均浏览文章时长(秒数),结果保留1位小数,并按时长由短到长排序 问题分解: 计算每次文章浏览的时长和日期: 过滤目标时间窗的有效浏览记录:WHERE artical_id != 0 AND DATE_FORMAT( 展开全文
头像 波文
发表于 2022-01-26 15:51:54
-- 统计2021年11月每天的人均浏览文章时长(秒数),结果保留1位小数,并按时长由短到长排序。 -- 1、过滤2021年11月数据 2、计算文章阅读时长 3、按日期分组统计,每天的人均浏览文章时长=当天阅读总时长/总人数 SELECT DATE_FORMAT(in_time, '% 展开全文
头像 JanisZhan
发表于 2022-06-23 16:02:42
select date_format(in_time,'%Y-%m-%d') as dt, round(sum(timestampdiff(second,in_time,out_time))/count(distinct uid),1) as avg_viiew_len_ 展开全文
头像 我超爱sql啦
发表于 2023-08-29 19:49:44
#1)筛选条件有2个,①日期是2021年11月,所以要将in_time字段转换成年+月形式的字符串,使用date_format(日期,日期格式)函数;②过滤atrticle_id不为0的数据; #2)根据日期分组,但日期本身是长日期格式,要转换成短日期,可以使用date(日期)函数; #3)人均浏览 展开全文
头像 酸菜鱼土豆大侠
发表于 2022-11-21 09:58:34
【场景】:每天人均 【分类】:嵌套子查询、日期函数、month、year、date、date_format 分析思路 难点: 1.难点:计算人均浏览时长时,可能一个人当天浏览多条记录,所以需要对用户去重后统计个数 (1)统计2021年11月每天的人均浏览文章时长(秒数),结果保留1位小数,并按时 展开全文
头像 和数学相爱相杀
发表于 2021-12-28 22:44:33
select date(in_time) dt, round(sum(timestampdiff(second, in_time, out_time))/count(distinct uid),1) avg_view_len_sec from tb_user_log where date_for 展开全文
头像 牛客716733975号
发表于 2023-05-25 13:14:21
select group_concat(distinct DATE_FORMAT(in_time, '%Y-%m-%d')) dt, round(sum(TIMESTAMPDIFF(second, in_time, out_time))/ count(distinct uid), 1) 展开全文
头像 时光舞者
发表于 2023-01-09 16:39:51
select date_format(in_time,'%Y-%m-%d') dt, round(sum(timestampdiff(second,in_time,out_time))/count(distinct uid),1) avg_viiew_len_sec from tb_user_log 展开全文
头像 牛客574098914号
发表于 2022-04-26 21:16:05
select date_format(in_time,'%Y-%m-%d') as dt, round(sum(timestampdiff(second, in_time, out_time)) / count(distinct uid), 1) as avg_viiew_len_sec fr 展开全文
  • 二维码

    扫描二维码,关注牛客网

  • 二维码

    下载牛客APP,随时随地刷题