Kathan Vasani- CRED Assignment
Kathan Vasani- CRED Assignment
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)
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 Question 4)
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
Hence most (530 out of 962 zombies) stopped using platform because
i)transaction_status: not complete (and/or)
ii)delivery_status:not delivered
As we can see using the following query that some (product_taste,product_size) sell very less,
hence they can be given as freebies
Query: