Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

13 游戏玩法分析 I #18

Open
astak16 opened this issue Jan 10, 2022 · 0 comments
Open

13 游戏玩法分析 I #18

astak16 opened this issue Jan 10, 2022 · 0 comments
Labels

Comments

@astak16
Copy link
Owner

astak16 commented Jan 10, 2022

题目

获取每位玩家第一次登录平台的日期

create table activity (
	player_id int,
	device_id int,
	event_date date,
	games_played int
);

insert into activity values
(1, 2, '2016-03-01', 5),
(1, 2, '2016-05-02', 6),
(1, 3, '2017-06-25', 1),
(3, 1, '2016-03-02', 0),
(3, 4, '2018-07-03', 5);

SQL:方法一

select player_id, min(event_date) as first_login from activity
group by player_id;

解析

  • 按照 player_idactivity 分组
  • 使用 min 函数,求出日期的最小值

SQL:方法二

select player_id, event_date as first_login from (
	select
		player_id,
		event_date,
		dense_rank()
		 over(partition by player_id order by event_date) as 排名
	from activity
) as temp where 排名 = 1;

解析

  • 使用 dense_rank 函数按照 player_idevent_date 进行排序,并算出排名将其作为临时表 temp
  • 查询临时表 temp,筛选出 排名 = 1 数据
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant