1
+ # CREATE table activityy
2
+ (
3
+ user_id char (20 ),
4
+ event_name char (20 ),
5
+ event_date date ,
6
+ country char (20 )
7
+ );
8
+ # delete from activityy;
9
+ # insert into activityy values (1,'app-installed','2022-01-01','India')
10
+ ,(1 ,' app-purchase' ,' 2022-01-02' ,' India' )
11
+ ,(2 ,' app-installed' ,' 2022-01-01' ,' USA' )
12
+ ,(3 ,' app-installed' ,' 2022-01-01' ,' USA' )
13
+ ,(3 ,' app-purchase' ,' 2022-01-03' ,' USA' )
14
+ ,(4 ,' app-installed' ,' 2022-01-03' ,' India' )
15
+ ,(4 ,' app-purchase' ,' 2022-01-03' ,' India' )
16
+ ,(5 ,' app-installed' ,' 2022-01-03' ,' SL' )
17
+ ,(5 ,' app-purchase' ,' 2022-01-03' ,' SL' )
18
+ ,(6 ,' app-installed' ,' 2022-01-04' ,' Pakistan' )
19
+ ,(6 ,' app-purchase' ,' 2022-01-04' ,' Pakistan' );
20
+ # THe activity table shows the app-installed and app purchase activities fro spotify app along with country details
21
+ select * from activityy;
22
+ # Q.1 find totoal active users each day (Daily active users)
23
+ select event_date,count (distinct user_id) from activityy a
24
+ group by event_date;
25
+
26
+ # Q.2 find total active users each week (Weekly active users)
27
+ # ssms only
28
+ select * ,datepart(week,event_date) from activityy;
29
+
30
+ # Q.3 datewise total number of users who made the purchase same day they insatlled the app (same day install and purchase)
31
+ select user_id,event_date,count (distinct event_name) as no_of_events from activityy
32
+ group by user_id,event_date having (count (distinct event_name))= 2 ;
33
+ # or
34
+ select event_date,count (user_id) as no_of_users from (
35
+ select user_id,event_date,count (distinct event_name) as no_of_events from activityy
36
+ group by user_id,event_date having (count (distinct event_name))= 2 ) a
37
+ group by event_date;
38
+ # final answer
39
+ select event_date,count (new_user) as no_of_users from (
40
+ select user_id,event_date,case when (count (distinct event_name))= 2 then user_id else null end as new_user from activityy
41
+ group by user_id,event_date # having (count(distinct event_name))=2 ) a
42
+ ) a
43
+ group by event_date;
44
+
45
+ # Q4. percentage of paid users in INDIA,USA AND ANY OTHER country should be tagged as others (country wise paid users)
46
+ select case when country in (' India' ,' USA' ) then country else ' Others' end as new_country,count (distinct user_id) from activityy
47
+ where event_name = " app-purchase"
48
+ group by case when country in (' India' ,' USA' ) then country else ' Others' end ;
49
+ # cte
50
+ with country_wise_users as (
51
+ select case when country in (' India' ,' USA' ) then country else ' Others' end as new_country,count (distinct user_id) as user_cnt from activityy
52
+ where event_name = " app-purchase"
53
+ group by case when country in (' India' ,' USA' ) then country else ' Others' end ),
54
+ total as (select sum (user_cnt) as total_users from country_wise_users )
55
+ select
56
+ new_country,user_cnt* 1 .0 / total_users* 100 as percent_users
57
+ from country_wise_users,total;
58
+
59
+ # Q.5 among All the users who installed the apps on a given day,how many did in app purchased on the very next day --day wise result
60
+ select *
61
+ ,lag(event_name,1 ) over(partition by user_id order by event_date) as prev_event_name
62
+ ,lag(event_date,1 ) over(partition by user_id order by event_date) as prev_event_date
63
+ from activityy;
64
+ # only ssms
65
+ with prev_data as (
66
+ select *
67
+ ,lag(event_name,1 ) over(partition by user_id order by event_date) as prev_event_name
68
+ ,lag(event_date,1 ) over(partition by user_id order by event_date) as prev_event_date
69
+ from activityy)
70
+ select * from prev_data
71
+ where event_name= ' app-purchase' and prev_event_name= ' app-installed' and datedifff(day,prev_event_date,event_date)= 1
72
+
73
+ #
74
+ with prev_data as (
75
+ select *
76
+ ,lag(event_name,1 ) over(partition by user_id order by event_date) as prev_event_name
77
+ ,lag(event_date,1 ) over(partition by user_id order by event_date) as prev_event_date
78
+ from activityy)
79
+ select event_date,count (distinct user_id) as cnt_users from prev_data
80
+ where event_name= ' app-purchase' and prev_event_name= ' app-installed' and datedifff(day,prev_event_date,event_date)= 1
81
+ group by event_date
0 commit comments