leetcode1079:游戏玩法分析——求留存率

求留存率

题目描述

表:Activity

±-------------±--------+

| Column Name | Type |

±-------------±--------+

| player_id | int |

| device_id | int |

| event_date | date |

| games_played | int |

±-------------±--------+

(player_id,event_date)是此表的主键(具有唯一值的列的组合)

这张表显示了某些游戏的玩家的活动情况

每一行表示一个玩家的记录,在某一天使用某个设备注销之前,登录并玩了很多游戏(可能是 0)

玩家的 安装日期 定义为该玩家的第一个登录日。

我们将日期 x 的 第一天留存率 定义为:假定安装日期为 X 的玩家的数量为 N ,其中在 X 之后的一天重新登录的玩家数量为 M,M/N 就是第一天留存率,四舍五入到小数点后两位。

编写解决方案,报告所有安装日期、当天安装游戏的玩家数量和玩家的 第一天留存率。

以 任意顺序 返回结果表。

结果格式如下所示。

题解

t1:查询所有玩家首次安装游戏的时间

sql 复制代码
select
player_id,
min(event_date) as install_dt
from Activity
group by player_id

t2:查询当天安装该游戏的玩家数量

sql 复制代码
select t1.install_dt,
count(player_id) as installs
from 
    (select
    player_id,
    min(event_date) as install_dt
    from Activity
    group by player_id) t1

t3:查询安装第二天仍然登录游戏的玩家

sql 复制代码
select 
t1.install_dt,
count(a.player_id) as Day1_nums
from Activity a
left join 
    (select
    player_id,
    min(event_date) as install_dt
    from Activity
    group by player_id) t1
on a.event_date = date_add(t1.install_dt,interval 1 day)
and a.player_id = t1.player_id
where install_dt is not null
group by install_dt

将以上进行综合,并用t3中的第二天仍登录数量/首次登录的玩家数量,等于留存率。

即最终代码如下:

sql 复制代码
select t1.install_dt,
count(player_id) as installs,
ifnull(round(Day1_nums/count(player_id),2),0) as Day1_retention
from 
    (select
    player_id,
    min(event_date) as install_dt
    from Activity
    group by player_id) t1
left join
(
select 
t1.install_dt,
count(a.player_id) as Day1_nums
from Activity a
left join 
    (select
    player_id,
    min(event_date) as install_dt
    from Activity
    group by player_id) t1
on a.event_date = date_add(t1.install_dt,interval 1 day)
and a.player_id = t1.player_id
where install_dt is not null
group by install_dt) t3
on t1.install_dt = t3.install_dt
group by t1.install_dt

题目来源:力扣

相关推荐
encoding-console13 分钟前
欧拉环境(openEuler 22.03 LTS SP3)安装移动磐维数据库(PanWeiDB_V2.0-S2.0.2_B01)步骤
数据库·虚拟机·欧拉·磐维数据库
AI_RSER15 分钟前
基于 Google Earth Engine 的南京江宁区土地利用分类(K-Means 聚类)
算法·机器学习·分类·kmeans·聚类·遥感·gee
辰哥单片机设计26 分钟前
PTC加热片详解(STM32)
数据库·mongodb
二年级程序员32 分钟前
MySQL 事务(详细版)
数据库
Small踢倒coffee_氕氘氚32 分钟前
是否应该禁止危险运动论文
经验分享·笔记·算法·灌灌灌灌
东方佑1 小时前
使用Python创建带边框样式的Word表格
数据库·python·word
柳如烟@1 小时前
Docker安装ES :确保 Kibana 正确连接 Elasticsearch
运维·数据库·elasticsearch·docker·容器·kibana
快乐点吧1 小时前
【MongoDB】windows安装、配置、启动
数据库·windows·mongodb
yangmf20401 小时前
私有知识库 Coco AI 实战(二):摄入 MongoDB 数据
数据库·人工智能·mongodb·coco ai
努力进修2 小时前
【金仓数据库征文】金仓数据库:开启未来技术脑洞,探索数据库无限可能
数据库·金仓数据库 2025 征文·数据库平替用金仓