SQL统计次月复购率

 

-- 复购率
select zry 首次购买月份,zyhs 当月新增客户数,
max(case when fgy-zry=1 then fgyhs else null end) m1,
max(case when fgy-zry=2 then fgyhs else null end) m2,
max(case when fgy-zry=3 then fgyhs else null end) m3,
max(case when fgy-zry=4 then fgyhs else null end) m4,
max(case when fgy-zry=5 then fgyhs else null end) m5,
max(case when fgy-zry=6 then fgyhs else null end) m6,
max(case when fgy-zry=7 then fgyhs else null end) m7
from
(select zry,fgy,fgyhs,zyhs,round(fgyhs/zyhs,2) fgl
from 
(select t1.dt zry,t2.dt fgy,count(distinct t2.user_id) fgyhs 
from (select month(order_date) dt,user_id from order_detail where order_status='交易成功' group by month(order_date),user_id) t1 
join (select month(order_date) dt,user_id from order_detail where order_status='交易成功' group by month(order_date),user_id) t2 
on t1.user_id=t2.user_id and t1.dt < t2.dt 
group by t1.dt,t2.dt) a -- 号码加小于本身时间连接
join 
(select month(order_date) yf,count(distinct user_id) zyhs
from order_detail 
group by month(order_date)) b 
on a.zry=b.yf) a  group by zry,zyhs;

-- 复购人数
select zry 首次购买月份,zyhs 当月新增客户数,
max(case when fgy-zry=1 then fgl else null end) m1,
max(case when fgy-zry=2 then fgl else null end) m2,
max(case when fgy-zry=3 then fgl else null end) m3,
max(case when fgy-zry=4 then fgl else null end) m4,
max(case when fgy-zry=5 then fgl else null end) m5,
max(case when fgy-zry=6 then fgl else null end) m6,
max(case when fgy-zry=7 then fgl else null end) m7
from
(select zry,fgy,fgyhs,zyhs,round(fgyhs/zyhs,2) fgl
from 
(select t1.dt zry,t2.dt fgy,count(distinct t2.user_id) fgyhs 
from (select month(order_date) dt,user_id from order_detail where order_status='交易成功' group by month(order_date),user_id) t1 
join (select month(order_date) dt,user_id from order_detail where order_status='交易成功' group by month(order_date),user_id) t2 
on t1.user_id=t2.user_id and t1.dt < t2.dt 
group by t1.dt,t2.dt) a 
join 
(select month(order_date) yf,count(distinct user_id) zyhs
from order_detail 
group by month(order_date)) b 
on a.zry=b.yf) a  group by zry,zyhs;

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值