首页 > 数据库 >牛客——SQL166 每天的日活数及新用户占比

牛客——SQL166 每天的日活数及新用户占比

时间:2023-09-05 09:46:34浏览次数:40  
标签:11 10 00 SQL166 uid 01 牛客 活数 2021

描述

用户行为日志表tb_user_log

id uid artical_id in_time out_time sign_cin
1 101 9001 2021-10-31 10:00:00 2021-10-31 10:00:09 0
2 102 9001 2021-10-31 10:00:00 2021-10-31 10:00:09 0
3 101 0 2021-11-01 10:00:00 2021-11-01 10:00:42 1
4 102 9001 2021-11-01 10:00:00 2021-11-01 10:00:09 0
5 108 9001 2021-11-01 10:00:01 2021-11-01 10:00:50 0
6 108 9001 2021-11-02 10:00:01 2021-11-02 10:00:50 0
7 104 9001 2021-11-02 10:00:28 2021-11-02 10:00:50 0
8 106 9001 2021-11-02 10:00:28 2021-11-02 10:00:50 0
9 108 9001 2021-11-03 10:00:01 2021-11-03 10:00:50 0
10 109 9002 2021-11-03 11:00:55 2021-11-03 11:00:59 0
11 104 9003 2021-11-03 11:00:45 2021-11-03 11:00:55 0
12 105 9003 2021-11-03 11:00:53 2021-11-03 11:00:59 0
13 106 9003 2021-11-03 11:00:45 2021-11-03 11:00:55 0

(uid-用户ID, artical_id-文章ID, in_time-进入时间, out_time-离开时间, sign_in-是否签到)

问题:统计每天的日活数及新用户占比

新用户占比=当天的新用户数÷当天活跃用户数(日活数)。

如果in_time-进入时间out_time-离开时间跨天了,在两天里都记为该用户活跃过。

新用户占比保留2位小数,结果按日期升序排序。

输出示例

示例数据的输出结果如下

dt dau uv_new_ratio
2021-10-30 2 1.00
2021-11-01 3 0.33
2021-11-02 3 0.67
2021-11-03 5 0.40

解释:

2021年10月31日有2个用户活跃,都为新用户,新用户占比1.00;

2021年11月1日有3个用户活跃,其中1个新用户,新用户占比0.33;

输入示例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-10-31 10:00:00', '2021-10-31 10:00:09', 0),
  (102, 9001, '2021-10-31 10:00:00', '2021-10-31 10:00:09', 0),
  (101, 0, '2021-11-01 10:00:00', '2021-11-01 10:00:42', 1),
  (102, 9001, '2021-11-01 10:00:00', '2021-11-01 10:00:09', 0),
  (108, 9001, '2021-11-01 10:00:01', '2021-11-01 10:01:50', 0),
  (108, 9001, '2021-11-02 10:00:01', '2021-11-02 10:01:50', 0),
  (104, 9001, '2021-11-02 10:00:28', '2021-11-02 10:00:50', 0),
  (106, 9001, '2021-11-02 10:00:28', '2021-11-02 10:00:50', 0),
  (108, 9001, '2021-11-03 10:00:01', '2021-11-03 10:01:50', 0),
  (109, 9002, '2021-11-03 11:00:55', '2021-11-03 11:00:59', 0),
  (104, 9003, '2021-11-03 11:00:45', '2021-11-03 11:00:55', 0),
  (105, 9003, '2021-11-03 11:00:53', '2021-11-03 11:00:59', 0),
  (106, 9003, '2021-11-03 11:00:45', '2021-11-03 11:00:55', 0);

输出:

2021-10-31|2|1.00
2021-11-01|3|0.33
2021-11-02|3|0.67
2021-11-03|5|0.40

我的解题思路:

1.处理in_time-进入时间out_time-离开时间跨天问题,可以使用union all,后面再去重,这样就能统计到跨天的数据

select uid,
     date_format(in_time, '%Y-%m-%d') as c_date
from tb_user_log
union all
select uid,
     date_format(out_time, '%Y-%m-%d') as c_date
from tb_user_log

2.使用窗口函数dense_rank()来查找新用户,按照用户分组,日期降序排序,排在第一的说明该用户是第一次访问,即为新用户(可以使用if判断)

 select distinct uid,  # 记得去重
                 c_date,
                 if(dense_rank() over (partition by uid order by c_date) = 1, 1, 0) as num
 from (
          select uid,
                 date_format(in_time, '%Y-%m-%d') as c_date
          from tb_user_log
          union all
          select uid,
                 date_format(out_time, '%Y-%m-%d') as c_date
          from tb_user_log
      ) tt

3.对日期分组求和,得到日活和新用户占比

完整代码 :

select c_date,
       count(uid)                      as dau,
       round(sum(num) / count(uid), 2) as uv_new_ratio
from (
         select distinct uid,
                         c_date,
                         if(dense_rank() over (partition by uid order by c_date) = 1, 1, 0) as num
         from (
                  select uid,
                         date_format(in_time, '%Y-%m-%d') as c_date
                  from tb_user_log
                  union all
                  select uid,
                         date_format(out_time, '%Y-%m-%d') as c_date
                  from tb_user_log
              ) tt) t
group by c_date
order by c_date
;

其他解题思路:

看了下评价区,很多都是用的left join,但是作为一个Sql Boy,我认为很多计算第一时间都要想到窗口函数能不能解决,不能解决再想其他办法。
做SQL题的核心是理清一步一步的思路,这个思路就是你需要什么数据,然后怎么构造需要的数据

select 
    t1.dt dt,
    count(distinct t1.uid) dau,
    round(count(distinct t2.uid)/ count(distinct t1.uid),2) ub_new_ratio
from 
    (  -- 查找每天在线人的信息
        select
            uid,date(in_time) dt
        from tb_user_log
        union
        select
            uid,date(out_time) dt
        from tb_user_log
         
    ) t1 
    left join 
    (
        select  -- 查找每一天的新用户
            uid,min(date(in_time)) dt
        from tb_user_log
        group by uid
    ) t2
    on t1.uid=t2.uid and t1.dt=t2.dt
group by dt
order by dt
;

标签:11,10,00,SQL166,uid,01,牛客,活数,2021
From: https://www.cnblogs.com/moliyy/p/17678838.html

相关文章

  • 2023牛客多校训练营2
    B.LinkwithRailwayCompany最大权闭合子图问题,树链剖分建图求解简述最大权闭合子图:现有一有向图,所有点都有一个权值,你需要选择一个子图,使得子图所有点的出边都指向子图内部,问子图最大权考虑网络流,源点向所有正权点连流量为权值的边,所有负权点向汇点连流量为权值绝对值的边,......
  • 【牛客周赛 Round 10】A-D题解
    Ahttps://ac.nowcoder.com/acm/contest/64272/A题意游游定义一个数组为“稳定的”,当且仅当数组相邻的两个元素之差的绝对值不超过1。例如[2,3,2,2,1]是稳定的,而[1,3,2]则不是稳定的。游游拿到了一个数组,她想求出该数组的最长的“稳定的”连续子数组的长度。题解首先,如果在某......
  • 【牛客小白77】D 字符串哈希
    https://ac.nowcoder.com/acm/contest/64384/D题意给你一串长度为\(n(n\leq10^6)\)的密码,它是顺序输入的,如果截止到某一位,输入的最后\(m\)个字符是密码,那么之前输入的所有东西都清除。问目前检测到\(k(m*k\leqn)\)次输入成功,问密码可能的种类数思路很容易想到枚举......
  • 牛客小白月赛77 C题解 | 小Why的商品归位
    原题链接先不考虑车子的容量问题,因为结束位置保证是在起始位置之后的,那我们从前往后扫,发现是可以知道每个点时的车内的商品。但是现在有了容量限制,我们怎么办呢,如果对于一段,k都是大于每个点的货物量时,可以一趟装完,但是如果大于k就需要不知一次,可以发现所需的其实是该段的最大......
  • 牛客——SQL255 给出employees表中排名为奇数行的first_name
    描述对于employees表中,输出first_name排名(按first_name升序排序)为奇数的first_name输出格式:firstGeorgiAnneke请你在不打乱原序列顺序的情况下,输出:按first_name排升序后,取奇数行的first_name。如对以上示例数据的first_name排序后的序列为:Anneke、Bezalel......
  • 牛客练习赛114
    B题是纯数学期望推导,用到错位相减,注意数学式子推导过程中一些常数不要丢掉,由于式子其中一部分非常复杂导致计算出来后忘掉最初式子。c题待补D题是贪心,需要找到最优策略。策略是倒着推并且遇到当前数出现次数比他的出现次数多时就停下。不停下会导致多出现的呢个数没有数列带它走......
  • C-小美的01串翻转_牛客周赛 Round 9
    链接:https://ac.nowcoder.com/acm/contest/63869/C来源:牛客网题目描述小美定义一个01串的权值为:每次操作选择一位取反,使得相邻字符都不相等的最小操作次数。例如,"10001"的权值是1,因为只需要修改一次:对第三个字符取反即可。现在小美拿到了一个01......
  • 牛客——SQL165 统计活跃间隔对用户分级结果
    描述用户行为日志表tb_user_logiduidartical_idin_timeout_timesign_cin110990012021-08-3110:00:002021-08-3110:00:090210990022021-11-0411:00:552021-11-0411:00:590310890012021-09-0110:00:012021-09-0110:01:500410890012......
  • 牛客网——SQL218 获取所有非manager员工当前的薪水情况
    描述有一个员工表employees简况如下:emp_nobirth_datefirst_namelast_namegenderhire_date100011953-09-02GeorgiFacelloM1986-06-26100021964-06-02BezalelSimmelF1996-08-03有一个,部门员工关系表dept_emp简况如下:emp_nodept_nofrom_date......
  • 2023牛客暑期多校练营6 A-Tree 树上背包+并查集
    2023牛客暑期多校练营6A-Tree树上背包+并查集题目链接题意:给出一棵树,节点为黑色或者白色,定义整棵树的贡献为,任意白点到任意黑点所经过路径上的最大边权之和,节点i原本颜色已给出,可以花费c[i]代价翻转节点i的颜色,问最大贡献是多少。做法:首先我们思考怎么处理最大边权的问题......