Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 2891a89

Browse files
authoredFeb 11, 2024
Create 01097-game-play-analysis-v.sql
1 parent c3e6987 commit 2891a89

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed
 

‎01097-game-play-analysis-v.sql

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
-- cte- get min date(install date) in the same table using window fun
2+
-- then use CASE to get flag where diff between days is 1
3+
-- in the final query, do a sum on flag divided by count of distinct players
4+
-- group by install date
5+
6+
with cte as
7+
(select *,
8+
min(event_date) over(partition by player_id) as first_date
9+
from Activity),
10+
11+
cte2 as
12+
(select a.player_id, a.event_date, a.first_date,
13+
(case when datediff(a.event_date, a.first_date) = 1 then 1 else 0 end) flg
14+
from cte a)
15+
16+
select first_date as install_dt,
17+
count(distinct player_id) as installs,
18+
round(sum(flg)/count(distinct player_id), 2) as Day1_retention
19+
from cte2
20+
group by 1
21+
22+
-- o/p of cte2
23+
24+
| player_id | event_date | first_date | flg |
25+
| --------- | ---------- | ---------- | --- |
26+
| 1 | 2016-03-01 | 2016-03-01 | 0 |
27+
| 1 | 2016-03-02 | 2016-03-01 | 1 |
28+
| 2 | 2017-06-25 | 2017-06-25 | 0 |
29+
| 3 | 2016-03-01 | 2016-03-01 | 0 |
30+
| 3 | 2018-07-03 | 2016-03-01 | 0 |
31+
32+
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
33+
-- only change in row 39 from the above code
34+
35+
with get_min_dts as
36+
(select *,
37+
min(event_date) over(partition by player_id) as install_dt
38+
from Activity),
39+
40+
cte2 as
41+
(select install_dt,
42+
count(distinct player_id) as cnt_all,
43+
sum(datediff(event_date, install_dt) = 1) as cnt_retended,
44+
sum(datediff(event_date, install_dt) = 1)/ count(distinct player_id) as Day1_retention
45+
from get_min_dts
46+
group by install_dt)
47+
48+
select install_dt, cnt_all as installs, round(Day1_retention, 2) as Day1_retention
49+
from cte2
50+
51+
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
52+
-- concised version of the above
53+
54+
with get_min_dts as
55+
(select *,
56+
min(event_date) over(partition by player_id) as install_dt
57+
from Activity)
58+
59+
select install_dt,
60+
count(distinct player_id) as installs,
61+
round(sum(datediff(event_date, install_dt) = 1)/ count(distinct player_id), 2) as Day1_retention
62+
from get_min_dts
63+
group by install_dt
64+
65+
66+
-- gsn games- 1

0 commit comments

Comments
 (0)
Please sign in to comment.