题解 | #零食类商品中复购率top3高的商品#
零食类商品中复购率top3高的商品
https://www.nowcoder.com/practice/9c175775e7ad4d9da41602d588c5caf3
### 得到每个产品每个用户ID 在90天内的购买次数,所谓的购买总人数是指的 符合时间规定在90天内发生了购买的总人数,所谓的 复购至少为2次的,是指的购买的日期符合90天内的购买次数至少为2次。 统计基表数据,然后根据product_id 分组得到了 超过2次的总人数和 每个产品的购买总人数。
select product_id,
round(count(if(repurCnt>=2,repurCnt,null))/count(uid),3) repurchase_rate
from (
select td.product_id ,
too.uid ,
#每个产品每个uid下购买的次数
count(if(datediff((select max(event_time) from tb_order_overall where status = 1),too.event_time)+1<=90,too.uid,null)) repurCnt
from tb_order_detail td
join tb_product_info ti using(product_id)
join tb_order_overall too using(order_id)
where ti.tag = '零食'
and too.status = 1
group by td.product_id,too.uid
) A
group by A.product_id
order by repurchase_rate desc, A.product_id
limit 3 ;
查看10道真题和解析