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

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

https://www.nowcoder.com/practice/9c175775e7ad4d9da41602d588c5caf3?tpId=268&tqId=2285517&sourceUrl=%2Fexam%2Foj%3FquestionJobId%3D10%26subTabName%3Donline_coding_page


SELECT 
    product_id,
    ROUND(repurchases / NULLIF(users, 0), 3) AS repurchase_rate
FROM (
    SELECT 
        product_id,
        COUNT(DISTINCT uid) AS users,
        COUNT(DISTINCT CASE WHEN ttl > 1 THEN uid END) AS repurchases
    FROM (
        SELECT 
            b.product_id, 
            a.uid, 
            COUNT(distinct a.order_id) AS ttl,
            MAX(DATE(event_time)) AS max_date,
            MIN(DATE(event_time)) AS min_date
        FROM 
            tb_order_overall a 
        JOIN 
            tb_order_detail b ON a.order_id = b.order_id 
        JOIN tb_product_info c ON b.product_id = c.product_id 
        and c.tag = "零食"
        WHERE 
            a.status =1
        and a.event_time >=  (
        SELECT DATE_SUB(MAX(event_time), INTERVAL 89 DAY)
        FROM tb_order_overall
    )
        GROUP BY 
            b.product_id, a.uid
    ) t1
    GROUP BY 
        product_id
) t2
ORDER BY 
    repurchase_rate DESC, 
    product_id ASC 
LIMIT 3;

全部评论

相关推荐

点赞 评论 收藏
分享
牛客52811839...:实习要写出来业务和产出,你这写的像流水账没人看。项目经历也没有,换个极简简历试试
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务