• LeetCode 1142.过去30天的用户活动2


    数据准备

    drop table Activity;
    Create table If Not Exists Activity (user_id int, session_id int, activity_date date, activity_type ENUM('open_session', 'end_session', 'scroll_down', 'send_message'));
    Truncate table Activity;
    insert into Activity (user_id, session_id, activity_date, activity_type) values ('1', '1', '2019-07-20', 'open_session');
    insert into Activity (user_id, session_id, activity_date, activity_type) values ('1', '1', '2019-07-20', 'scroll_down');
    insert into Activity (user_id, session_id, activity_date, activity_type) values ('1', '1', '2019-07-20', 'end_session');
    insert into Activity (user_id, session_id, activity_date, activity_type) values ('2', '4', '2019-07-20', 'open_session');
    insert into Activity (user_id, session_id, activity_date, activity_type) values ('2', '4', '2019-07-21', 'send_message');
    insert into Activity (user_id, session_id, activity_date, activity_type) values ('2', '4', '2019-07-21', 'end_session');
    insert into Activity (user_id, session_id, activity_date, activity_type) values ('3', '2', '2019-07-21', 'open_session');
    insert into Activity (user_id, session_id, activity_date, activity_type) values ('3', '2', '2019-07-21', 'send_message');
    insert into Activity (user_id, session_id, activity_date, activity_type) values ('3', '2', '2019-07-21', 'end_session');
    insert into Activity (user_id, session_id, activity_date, activity_type) values ('3', '5', '2019-07-21', 'open_session');
    insert into Activity (user_id, session_id, activity_date, activity_type) values ('3', '5', '2019-07-21', 'scroll_down');
    insert into Activity (user_id, session_id, activity_date, activity_type) values ('3', '5', '2019-07-21', 'end_session');
    insert into Activity (user_id, session_id, activity_date, activity_type) values ('4', '3', '2019-06-25', 'open_session');
    insert into Activity (user_id, session_id, activity_date, activity_type) values ('4', '3', '2019-06-25', 'end_session');
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    需求

    编写 SQL 查询以查找截至 2019-07-27(含)的 30 天内每个用户的平均会话数,四舍五入到小数点后两位。只统计那些会话期间用户至少进行一项活动的有效会话。

    输入

    在这里插入图片描述

    输出

    with t1 as (
        select user_id,session_id
        from Activity
        where activity_date<='2019-07-27' and activity_date>date_sub('2019-07-27',interval 30 day )
        group by user_id,session_id
    ),t2 as (
        select user_id,count(1) as cnt1
        from t1
        group by user_id
    )
    select round(sum(cnt1)/count(user_id),2) as average_sessions_per_user
    from t2;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    在这里插入图片描述

  • 相关阅读:
    Docker从入门到进阶之进阶操作(3) —— 使用 supermin5来构建镜像
    【sql】笔记大屏展示数据查询
    自学Python07-学会用Python读取Json 文件
    java基础-并发编程-CountDownLatch(JDK1.8)源码学习
    Hiredis的基本使用
    MATLAB 与 Cruise 的联合仿真
    总结了200道经典的机器学习面试题(附参考答案)
    漏刻有时数据可视化Echarts组件开发(27):盒须图(箱线图)前后端php交互的实战案例
    Open3d数据滤波和点云分割
    C#中接口的显式实现与隐式实现及其相关应用案例
  • 原文地址:https://blog.csdn.net/weixin_51696882/article/details/132869262