Skip to content

Commit bab06f3

Browse files
committed
adding sql prblems
1 parent 93a76e4 commit bab06f3

4 files changed

+126
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#create table bms (seat_no int ,is_empty varchar(10));
2+
#insert into bms values(1,'N'),(2,'Y'),(3,'N'),(4,'Y'),(5,'Y',(6,'Y'),(7,'N'),(8,'Y'),(9,'Y'),(10,'Y'),(11,'Y'),(12,'N'),(13,'Y'),(14,'Y');
3+
#3 OR MORE Consecutive Empty Seats
4+
#METHOD 1 - LEAD/LAG
5+
select * from (
6+
SELECT *
7+
,LAG(is_empty,1) over(order by seat_no) as prev_1
8+
,LAG(is_empty,2) over(order by seat_no) as prev_2
9+
,Lead(is_empty,1) over(order by seat_no) as next_1
10+
,Lead(is_empty,2) over(order by seat_no) as next_2
11+
FROM BMS) A
12+
where is_empty = 'Y' and prev_1 = 'Y' and prev_2 = 'Y'
13+
or (is_empty = 'Y' and prev_1 = 'Y' and next_1 = 'Y')
14+
or (is_empty = 'Y' and next_1 = 'Y' and next_2 = 'Y') ;
15+
16+
#method-2 Advance Aggregation
17+
select * from (
18+
SELECT *
19+
,sum(case when is_empty='Y' then 1 else 0 end) over(order by seat_no rows between 2 preceding and current row) as prev_2
20+
,sum(case when is_empty='Y' then 1 else 0 end) over(order by seat_no rows between 1 preceding and 1 following) as prev_next_1
21+
,sum(case when is_empty='Y' then 1 else 0 end) over(order by seat_no rows between current row and 2 following) as next_2
22+
FROM BMS) a
23+
where prev_2=3 or prev_next_1=3 or next_2=3;
24+
25+
#Method 3: Using ROW_NUMBER
26+
with diff_num as (
27+
select *
28+
,row_number() over(order by seat_no) as rn
29+
,seat_no-row_number() over(order by seat_no) as diff
30+
from bms where is_empty='Y'),
31+
cnt as (
32+
select diff, count(1) as c from diff_num
33+
group by diff having count(1)>=3)
34+
select * from diff_num where diff in (select diff from cnt) ;
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
#How to generate match schedule for list of countries | Match one row with all other rows
22
#create table countries (
3-
countryname varchar(20)
4-
);
3+
#countryname varchar(20)
4+
#);
55
#dont match o the same values
66
#insert into countries values('NewZealand'); #,'Australia','England','NewZealand')
77
select * from countries a
88
inner join Countries b
9-
on a.countryname<b.countryname #it will eliminate one of the duplicate alpbabeticall les than b.countryname
9+
on a.countryname<b.countryname #it will eliminate one of the duplicate alpbabeticall les than b.countryname
10+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#declare @var char(50) = 'abc,,def,,,,,,,,ghi,jkl,,,,,mno';
2+
#above is for ssms
3+
4+
SET @var = 'abc,,def,,,,,,,,ghi,jkl,,,,,mno';
5+
#select replace(@var, ',' , '*,') ;#every comma with * comma
6+
#select replace(replace(@var, ',' , '*,'), ',*','');
7+
select replace(replace(replace(@var, ',' , '*,'), ',*',''), '*,', ',') as ans;

0 commit comments

Comments
 (0)