平均活跃天数和月活人数(sql练习)

用户在牛客试卷作答区作答记录存储在表exam_record中,内容如下:

exam_record表(uid用户ID, exam_id试卷ID, start_time开始作答时间, submit_time交卷时间, score得分)

iduidexam_idstart_timesubmit_timescore
1100190012021-07-02 09:01:012021-07-02 09:21:0180
2100290012021-09-05 19:01:012021-09-05 19:40:0181
3100290022021-09-02 12:01:01(NULL)(NULL)
4100290032021-09-01 12:01:01(NULL)(NULL)
5100290012021-07-02 19:01:012021-07-02 19:30:0182
6100290022021-07-05 18:01:012021-07-05 18:59:0290
7100390022021-07-06 12:01:01(NULL)(NULL)
8100390032021-09-07 10:01:012021-09-07 10:31:0186
9100490032021-09-06 12:01:01(NULL)(NULL)
10100290032021-09-01 12:01:012021-09-01 12:31:0181
11100590012021-09-01 12:01:012021-09-01 12:31:0188
12100690022021-09-02 12:11:012021-09-02 12:31:0189
13100790022020-09-02 12:11:012020-09-02 12:31:0189

请计算2021年每个月里试卷作答区用户平均月活跃天数avg_active_days和月度活跃人数mau,上面数据的示例输出如下:

monthavg_active_daysmau
2021071.502
2021091.254

解释:2021年7月有2人活跃,共活跃了3天(1001活跃1天,1002活跃2天),平均活跃天数1.5;2021年9月有4人活跃,共活跃了5天,平均活跃天数1.25,结果保留2位小数。

注:此处活跃指有交卷行为。

示例1

输入:

drop table if exists exam_record;
CREATE TABLE exam_record (
    id int PRIMARY KEY AUTO_INCREMENT COMMENT '自增ID',
    uid int NOT NULL COMMENT '用户ID',
    exam_id int NOT NULL COMMENT '试卷ID',
    start_time datetime NOT NULL COMMENT '开始时间',
    submit_time datetime COMMENT '提交时间',
    score tinyint COMMENT '得分'
)CHARACTER SET utf8 COLLATE utf8_general_ci;

INSERT INTO exam_record(uid,exam_id,start_time,submit_time,score) VALUES
(1001, 9001, '2021-07-02 09:01:01', '2021-07-02 09:21:01', 80),
(1002, 9001, '2021-09-05 19:01:01', '2021-09-05 19:40:01', 81),
(1002, 9002, '2021-09-02 12:01:01', null, null),
(1002, 9003, '2021-09-01 12:01:01', null, null),
(1002, 9001, '2021-07-02 19:01:01', '2021-07-02 19:30:01', 82),
(1002, 9002, '2021-07-05 18:01:01', '2021-07-05 18:59:02', 90),
(1003, 9002, '2021-07-06 12:01:01', null, null),
(1003, 9003, '2021-09-07 10:01:01', '2021-09-07 10:31:01', 86),
(1004, 9003, '2021-09-06 12:01:01', null, null),
(1002, 9003, '2021-09-01 12:01:01', '2021-09-01 12:31:01', 81),
(1005, 9001, '2021-09-01 12:01:01', '2021-09-01 12:31:01', 88),
(1006, 9002, '2021-09-02 12:11:01', '2021-09-02 12:31:01', 89),
(1007, 9002, '2020-09-02 12:11:01', '2020-09-02 12:31:01', 89);

输出:

202107|1.50|2
202109|1.25|4

本题涉及sql知识的综合运用,需要注意的点有:题中的月活跃天数,需要考虑到日期,有些记录的日期是一样的,但是答题的时间是不一样的,如果这些记录的uid是一样的,则是同一个用户的同一天,不能算成是多天,这种情况需要用到DATE()函数获得submit_time的日期,并对其使用group by进行去重;输出要求年月中的月保持两位输出,可以用datepart返回需要的格式,也可以像例中的concat()函数和if()函数判断返回月份的尾数并进行拼接。

select
    b.month,
    round(active_days/count(*),2) avg_active_days,
    count(*) mau
from
    (select
        month,
        count(*) active_days
    from
        (select
            uid,
            concat('2021',if(length(month(submit_time))=1,concat('0',month(submit_time)),month(submit_time))) month
        from
            (select
                uid,
                date(submit_time) submit_time
            from
                exam_record
            where
                year(submit_time)=2021 and submit_time is not null) newt
        group by
            uid,submit_time) a
    group by month) b
    left join
        (select
            month,
            uid,
            count(*) active_user
        from
            (select
                uid,
                concat('2021',if(length(month(submit_time))=1,concat('0',month(submit_time)),month(submit_time))) month
            from
                (select
                    uid,
                    date(submit_time) submit_time
                from
                    exam_record
                where
                    year(submit_time)=2021 and submit_time is not null) newt
            where
                year(submit_time)=2021 and submit_time is not null
            group by
                uid,submit_time) a
        group by
            month,uid) c
    on b.month=c.month
group by
    b.month,active_days
order by
    month
  • 15
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值