Cohort Analysis by month - MySQL -
hi trying cohort analysis year 2017. have following 2 tables
firstpurchases - determine first purchase client has made greater 1. reason have credit card verification purchases less 1 usd upon first transaction.
transaction - table records purchasing , refund transactions customers.
the sql have written following
drop table if exists `firstpurchases`; create table `firstpurchases` ( select account_id,min(created_date) transaction_date `transaction` transaction.amount > 1 group account_id ); delete `firstpurchases` year(transaction_date) = 2016; select u.monthoffset month, sum(s.monthoffset = 0) d0, sum(s.monthoffset = 1) d1, sum(s.monthoffset = 2) d2, sum(s.monthoffset = 3) d3, sum(s.monthoffset = 4) d4, sum(s.monthoffset = 5) d5, sum(s.monthoffset = 6) d6, sum(s.monthoffset = 7) d7, sum(s.monthoffset = 8) d8, sum(s.monthoffset = 9) d9, sum(s.monthoffset = 10) d10, sum(s.monthoffset = 11) d11, sum(s.monthoffset = 12) d12 ( select account_id, month(transaction_date) monthoffset `firstpurchases` transaction_date >= curdate() - datediff(curdate(),'2017-01-01') ) u left join ( select distinct transaction.account_id, period_diff(date_format(transaction.created_date,'%y%m'),date_format(firstpurchases.transaction_date,'%y%m')) monthoffset `transaction` left join `firstpurchases` on (transaction.account_id = `firstpurchases`.account_id) `transaction`.created_date >= curdate() - datediff(curdate(),'2017-01-01') ) s on s.account_id = u.account_id group u.monthoffset;
although confident d0 correct. rest of numbers feel inflated me. have gone through exercise manually , figures dont match up. can maybe cast eye , see went wrong ? appreciated.
so turns out bit of dumbass , query correct. forgot filter out refunds in left join. if wants use template cohort analysis there u go :)
Comments
Post a Comment