0% found this document useful (0 votes)
3 views13 pages

Kathan Vasani- CRED Assignment

The document contains SQL queries related to user payment analytics, focusing on user transactions, payment statuses, and user engagement metrics. It also includes a case study on 'zombie' users who have not transacted in the last six months, analyzing their behavior and suggesting strategies for re-engagement. The document concludes with recommendations for offering freebies to incentivize these inactive users to return.

Uploaded by

kathanjvasani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views13 pages

Kathan Vasani- CRED Assignment

The document contains SQL queries related to user payment analytics, focusing on user transactions, payment statuses, and user engagement metrics. It also includes a case study on 'zombie' users who have not transacted in the last six months, analyzing their behavior and suggesting strategies for re-engagement. The document concludes with recommendations for offering freebies to incentivize these inactive users to return.

Uploaded by

kathanjvasani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Kathan Vasani

CRED Assignment Product Analytics

Question 1

WITH ordered_payments AS (
SELECT u.id AS User_id,
u.first_name,
u.last_name,
u.gender,
u.email,
cast(u.created_at as datetime) + interval '330' minute AS signup_date,
cast(o.created_at as datetime) + interval '330' minute AS payment_date,
o.amount,
o.status,
ROW_NUMBER() OVER (PARTITION BY u.id ORDER BY o.created_at) AS rn_first_payment,
ROW_NUMBER() OVER (PARTITION BY u.id ORDER BY o.created_at DESC) AS rn_last_payment
FROM public.user_schema_users u
LEFT JOIN public.wallet_service_orders o ON u.id = o.user_id)
SELECT User_id,
first_name,
last_name,
gender,
email,
signup_date,
--First payment details (any status)
MIN(CASE WHEN rn_first_payment = 1 THEN payment_date END) AS first_payment_date,
MIN(CASE WHEN rn_first_payment = 1 THEN status END) AS first_payment_status,
MIN(CASE WHEN rn_first_payment = 1 THEN amount END) AS first_payment_amount,
-- Last payment details (any status)
MAX(CASE WHEN rn_last_payment = 1 THEN payment_date END) AS last_payment_date,
MAX(CASE WHEN rn_last_payment = 1 THEN status END) AS last_payment_status,
MAX(CASE WHEN rn_last_payment = 1 THEN amount END) AS last_payment_amount,
-- Total amount paid (only for completed payments)
COALESCE(SUM(CASE WHEN status = 'completed' THEN amount END), 0) AS total_amount_paid
from ordered_payments
group by User_id, first_name,last_name,gender,email,signup_date
order by user_id
^(For Question 1)
Question 2

select count(*)
from (select id,b.total_amount
from user_schema_users as a
left join (select user_id,
sum(case when status = 'completed' then amount else 0 end)
as total_amount
from public.wallet_service_orders
group by 1) as b
on a.id=b.user_id
where b.total_amount is null or b.total_amount=0)

^(For Question 2)

● For the above query:


○ Some user_id in user_schema_users may not be present in
wallet_service_orders as they may have never transacted, hence have to count
them too, hence we have to perform a left join
Question3

select count(*)
from (select id,b.total_amount
from user_schema_users as a
left join (select user_id, sum(case when status = 'completed' then 1
else 0 end) as total_amount
from public.reward_service_reward_usages
group by 1) as b
on a.id=b.user_id
where b.total_amount is null or b.total_amount=0)

^(For Question 3)

● For the above query:


○ Some user_id in user_schema_users may not be present in
public.reward_service_reward_usages as they may have never been rewarded,
hence to count them too, we have to perform a left join.
Question 4

select cast(created_at as date) as date,sum(b.final_amount)


from public.wallet_service_orders a
left join (select cast(created_at as date) as previous_date,
(case when status = 'completed' then amount else 0 end) as
final_amount
from public.wallet_service_orders) b
on a.created_at>=b.previous_date
group by 1
order by 1

^(For Question 4)

● For the above query:


○ Transaction amount is only added to total if status = ‘completed’ else amount not
added towards the total.
Question 5

with daily_activity as (select distinct user_id, cast(created_at as date) as


activity_date
from public.wallet_service_orders ),
DAU as (select cast(created_at as date), COUNT(distinct DA.USER_ID) as
daily_users
from public.wallet_service_orders A
left join DAILY_ACTIVITY DA
ON cast(A.CREATED_AT as DATE)= DA.ACTIVITY_DATE
group by 1
order by 1),
WAU as (select cast(created_at as date), COUNT(distinct DA.USER_ID) as
weekly_users
from public.wallet_service_orders B
left join DAILY_ACTIVITY DA
on DA.ACTIVITY_DATE>= CAST(B.CREATED_AT as DATE)- interval '6
DAYS'
and DA.ACTIVITY_DATE <= cast(B.CREATED_AT as date)
group by 1
order by 1),
MAU as (select cast(created_at as date), COUNT (distinct DA.USER_ID) as
monthly_users
from public.wallet_service_orders C
left join DAILY_ACTIVITY DA
on DA.ACTIVITY_DATE>= CAST(C.CREATED_AT as DATE)- interval '29
DAYS'
and DA.ACTIVITY_DATE <= cast(C.CREATED_AT as date)
group by 1
order by 1)
select DAU.Created_at, DAU.daily_users,WAU.weekly_users,MAU.monthly_users
from DAU left join WAU on DAU.created_at=WAU.created_at left join MAU on
DAU.created_at=MAU.created_at
order by 1
^(For Question 5)

*case study starts next page


Case Study

1. I have defined Zombies as users that have not transacted in the last 6 months. The
following query gives a detailed description of the zombie information including but not
limited to - user_id, last_transaction_date,etc.

***I have taken the date '2022-03-31' as my reference point as that’s the latest date in
the table public.case_transactions

select cu.user_id,sign_up_date,
Age,gender,city,income,
os,device_make,acquisition_source,active_days,
max(to_date(txn_timestamp, 'DD/MM/YY')) as last_transaction_date
from public.case_users cu left join public.case_transactions ct
on cu.user_id= ct.user_id
group by 1,2,3,4,5,6,7,8,9,10
having max(to_date(txn_timestamp, 'DD/MM/YY'))> cast('2022-03-31' as date)-interval
'180'day

Following is the information obtained from the table: Total Zombies= 962
2. Yes, Zombies are real issue to the company
a. As they are ~28% (Current User Base = 3485)of the current user base of the
company.
b. They were ~28% (User Base 6 months back= 3439) user base of company 6
months ago
c. Company has not increased the user base by a significant margin in the last 6
months

Query to find the current user_base:


select count (distinct user_id) from public.case_transactions

Query to find the user_base 6 months back:


select count (distinct user_id)
from public.case_transactions
where to_date(txn_timestamp, 'DD/MM/YY')< cast('2022-03-31' as date)-interval '180'day
3. Insights on Zombie behavior can be drawn based on the last day transaction details of
Zombies
a. On looking at the last day transactions of zombies, it was found that there were a
lot of transactions for which the txn_status is not completed
(txn_status!=’completed) or the delivery_status is not delivered
(delivery_status!=’delivered’

● Total number of Transactions by Zombies on their last day= 1375


● Total number of transaction not complete/not delivered= 761
● Total users affected= 530
● Total Zombies=962

Hence most (530 out of 962 zombies) stopped using platform because
i)transaction_status: not complete (and/or)
ii)delivery_status:not delivered

Details of these transactions can be found using the following query:


with zombies as (Select
cu.user_id,sign_up_date,age,gender,city,income,os,device_make,acquisition_source,activ
e_days,
max(to_date(txn_timestamp, 'DD/MM/YY')) as last_transaction_date
from public.case_users cu
left join public.case_transactions ct on cu.user_id= ct.user_id
group by 1,2,3,4,5,6,7,8,9,10
having max(to_date(txn_timestamp, 'DD/MM/YY'))< cast('2022-03-31' as date)-interval
'180'day)
,
cte2 as (select
zombies.*,ct.amount,num_items,discount,txn_status,delivery_status,product_id,txn_id,pa
yment_mode
from zombies left join public.case_transactions ct on zombies.user_id=ct.user_id and
zombies.last_transaction_date= to_date(ct.txn_timestamp, 'DD/MM/YY')
)
Select *
from cte2

(Image attached on next page)


Query for Number of Users affected by Incomplete Deliveries/Incomplete Transactions
(530 users) :
with zombies as (Select
cu.user_id,sign_up_date,age,gender,city,income,os,device_make,acquisition_source,activ
e_days,
max(to_date(txn_timestamp, 'DD/MM/YY')) as last_transaction_date
from public.case_users cu
left join public.case_transactions ct on cu.user_id= ct.user_id
group by 1,2,3,4,5,6,7,8,9,10
having max(to_date(txn_timestamp, 'DD/MM/YY'))< cast('2022-03-31' as date)-interval
'180'day)
,
cte2 as (select
zombies.*,ct.amount,num_items,discount,txn_status,delivery_status,product_id,txn_id,pa
yment_mode
from zombies left join public.case_transactions ct on
zombies.user_id=ct.user_id and zombies.last_transaction_date=
to_date(ct.txn_timestamp, 'DD/MM/YY'))

Select count(distinct user_id)


from cte2
where txn_status!='completed' or delivery_status !='delivered'
4. Zombie Winback: Giving Freebies on Orders

As we can see using the following query that some (product_taste,product_size) sell very less,
hence they can be given as freebies

i) bitter-large ale’s, having lowest average_price=100 (others avg_price =1000/-) can be


given as a freebie along with an order
ii)bitter-regular and salty-regular: avg_price =1000 are slow moving and can be given as
freebies along with an order as well

Query:

with zombies as (Select


cu.user_id,sign_up_date,age,gender,city,income,os,device_make,acquisition_source,active_days,
max(to_date(txn_timestamp, 'DD/MM/YY')) as last_transaction_date
from public.case_users cu
left join public.case_transactions ct on cu.user_id= ct.user_id
group by 1,2,3,4,5,6,7,8,9,10
having max(to_date(txn_timestamp, 'DD/MM/YY'))>cast('2022-03-31' as date)-interval '180'day)
,
cte2 as (select
zombies.*,ct.amount,num_items,discount,txn_status,delivery_status,product_id,txn_id,payment_mo
de
from zombies left join public.case_transactions ct on zombies.user_id=ct.user_id and
zombies.last_transaction_date= to_date(ct.txn_timestamp, 'DD/MM/YY'))
,
cte3 as (select * from cte2 left join public.case_products cp
on cte2.product_id=cp.product_id)

Select product_taste, product_size,avg(product_price),count(*)


from cte3
group by 1,2

You might also like