1. 从表tb_product_info中筛选tag为零食的商品id形成表t1
  2. 从表tb_order_overall中筛选满足距离最大日期90天内并且status=1的商品订单形成表t3
select 
        order_id, uid 
    from 
        tb_order_overall 
    where 
        `status`=1 
        and 
        datediff((select max(event_time) from tb_order_overall), event_time)<90
  1. 依次使用left join 连接表t1、t2(tb_order_detail)、t3, 连接条件分别为:
t1.product_id=t2.product_id
t2.order_id=t3.order_id
  1. 使用group by对连接的表在product_id和uid上进行分组
  2. 利用开窗函数在每个product_id分组,count(*)为每个人购买的次数,使用if函数筛选用户购买次数为2次及2次以上的用户,满足条件为1,否则为0,最后利用sum汇总满足条件的总人数

sum(if(count(*)>=2, 1, 0)) over(partition by product_id)

特别注意:此处的count(*)为group by时统计每个每个product_id下每个uid购买的次数,发生在over之前over对应得窗口函数为sum

  1. 再次利用开窗函数在每个product_id分组,利用count(*)统计购买该件商品的总人数

特别注意:此处的count(*)为group by之后统计购买product_id的用户数,发生在over之后over对应得窗口函数为count

count(*) over(partition by product_id)

select
    distinct t1.product_id,
    round(sum(if(count(*)>=2, 1, 0)) over(partition by product_id) 
    / count(*) over(partition by product_id), 3) repurchase_rate
from
    (select 
        product_id
    from 
        tb_product_info 
    where tag='零食') t1
left join
    tb_order_detail t2
on t1.product_id=t2.product_id
left join 
    (select 
        order_id, uid 
    from 
        tb_order_overall 
    where 
        `status`=1 
        and 
        datediff((select max(event_time) from tb_order_overall), event_time)<90) t3
on t2.order_id=t3.order_id
group by
    t1.product_id, t3.uid
order by
    repurchase_rate desc, product_id
limit 3;