零食类商品中复购率top3高的商品
明确题意:
统计零食类商品中复购率top3高的商品。复购率指用户在一段时间内对某商品的重复购买比例,复购率越大,则反映出消费者对品牌的忠诚度就越高,也叫回头率。
此处我们定义:某商品复购率 = 近90天内购买它至少两次的人数 ÷ 购买它的总人数。近90天指包含最大日期(记为当天)在内的近90天。
结果中复购率保留3位小数,并按复购率倒序、商品ID升序排序。
问题分解:
- 计算每个用户对每个商品是否复购(生成子表t_uid_product_info):
- 内连接多表:
tb_order_detail JOIN tb_order_overall USING(order_id) JOIN tb_product_info USING(product_id)
- 筛选零食类商品:WHERE tag="零食"
- 筛选近90天的记录:
- 计算最小允许日期:DATE_SUB(MAX(event_time), INTERVAL 89 DAY)
- 筛选:event_time >= (SELECT ... FROM tb_order_overall)
- 按用户和商品分组:GROUP BY uid, product_id
- 计算是否复购:IF(COUNT(event_time)>1, 1, 0) as repurchase
- 内连接多表:
- 按商品分组:GROUP BY product_id
- 计算复购率:SUM(repurchase) / COUNT(repurchase) as repurchase_rate
- 保留3位小数:ROUND(x, 3)
细节问题:
- 表头重命名:as
- 按复购率倒序、商品ID升序排序:ORDER BY repurchase_rate DESC, product_id
- 保留top3高的结果:LIMIT 3;
完整代码:
SELECT product_id,
ROUND(SUM(repurchase) / COUNT(repurchase), 3) as repurchase_rate
FROM (
SELECT uid, product_id, IF(COUNT(event_time)>1, 1, 0) as repurchase
FROM tb_order_detail
JOIN tb_order_overall USING(order_id)
JOIN tb_product_info USING(product_id)
WHERE tag="零食" AND event_time >= (
SELECT DATE_SUB(MAX(event_time), INTERVAL 89 DAY)
FROM tb_order_overall
)
GROUP BY uid, product_id
) as t_uid_product_info
GROUP BY product_id
ORDER BY repurchase_rate DESC, product_id
LIMIT 3;