题解 | #零食类商品中复购率top3高的商品#

零食类商品中复购率top3高的商品

https://www.nowcoder.com/practice/9c175775e7ad4d9da41602d588c5caf3

# 821 # 10:45 - 

# 统计零食类商品中复购率top3高的商品

# 商品复购率 = 近90天内购买它至少两次的人数 ÷ 购买它的总人数

# 字段:product_id,repurchase_rate
# tb1:链接
with tb1 as(
    select product_id, date(event_time) as dt, uid
    from tb_order_detail left join tb_order_overall using(order_id)
    left join tb_product_info using(product_id)
    where tag = '零食' 
    AND status = 1
    AND date(event_time) >= (select date_sub(max(date(event_time)),interval 89 day) from tb_order_overall) # 最远日期2021-08-06
),

# tb2:统计复购人数
tb2 as(
select product_id,uid,count(dt) as pay_cnt
from tb1 
group by product_id,uid
)
# 注意点:一笔订单里面,购买两次同一个产品,不算复购(从结果来看)

# 结果查询:复购/总人数   复购人数= 购买次数大于2的人数   总人数直接count
select product_id,round(count(if(pay_cnt>=2,pay_cnt,null)) /count(pay_cnt),3) as repurchase_rate
from tb2
group by product_id
order by repurchase_rate desc,product_id asc
limit 3;

# 注意点:一笔订单里面,购买两次同一个产品,不算复购(从结果来看)
# 注意点:输出top3,limit 3




全部评论

相关推荐

程序员鼠鼠_春招版:都很烂大街,rpc也基本没人问,考研吧,不然就包装一段实习再去
点赞 评论 收藏
分享
新记话事人:你就和她说去抖音了
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务