本文中总结了LeetCode中关于SQL的游戏玩家分析的4个题目

有个activity表,记录了一些游戏玩家在游戏平台上的行为活动。

写一条SQL语句,获取每个玩家第一次登陆平台的日期,实现如下结果:

select play_id,min(event_date) as first_login
from activity
group by play_id;本题难度是很简单的,只需要使用min和group by 进行分组即可
min函数返回的最小值,不包含NULLGroup by函数根据一个或者多个列对结果进行分组有个activity表,记录了一些游戏玩家在游戏平台上的行为活动。

play_id,event_data0个)编写SQL语句,描述每一个玩家首次登陆的设备名称!
right jion解决select a.player_id, a.device_id from activity a
right join (select play_er, min(event_date) as latest_date
from activity
group by play_id) b -- 将上面的结果作为表b
on a.event_date = b.latest_date
where a.player_id = b.play_id;select a.play.id, a.device_id
from activity a
where (a.play_id, a.event_date) in (select play_id, min(event_date) as first_login
from activity
group by player_id); --将子查询的结果作为条件,in关键字可以包含两个字段信息511题目的结果作为中间一个表格rigth join on相当于将右边的结果作为左边的条件有个activity表,记录了一些游戏玩家在游戏平台上的行为活动。

play_id,event_data编写SQL语句,同时报告每组玩家和日期,以及玩家到此为止玩了多少场游戏,也就是此日期之前的游戏总数。
select a.player, a.event_date, (select sum(b.games_played) -- 求和函数sum()
from activity b
where a.play_id=b.play_id
and a.event_date=b.event_date) as b "games_played_as_far" -- 通过临时表筛选出此日期前的游戏总数
from activity as a;题目同上,编写的SQL语句要求不同:首次登陆的第二天再次登录的玩家的分数,四色五入到小数点后两位
round函数select round((select count(distinct b.play_id)
from activity as b, activity as c
where b.event_date=(select min(temp.event_date)
from activity as temp.play_id=b.play_id)
and b.play_id=play_id
and datediff(b.event_date, c.event_date=-1)/count(distinct a.play_id), 2) as "fraction"
from activity as a;