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

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

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

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

select
    product_id,
    # 某商品复购率 = 近90天内购买它至少两次的人数 ÷ 购买它的总人数
    round(count(distinct uid) / num, 3) repurchase_rate
from
    (
        select
        #  近90天内购买它至少两次的人数
            product_id,
            uid,
            count(*) repurchase_num
        from
            tb_product_info
            join tb_order_detail using (product_id)
            join tb_order_overall using (order_id)
        where
            datediff (
                (
                    select
                        max(event_time)
                    from
                        tb_order_overall
                ),
                event_time
            ) < 90
            and tag = '零食'
        group by
            product_id,
            uid
        having
            repurchase_num >= 2
    ) a
    right join (
        #  购买它的总人数
        select
            product_id,
            count(distinct uid) num
        from
            tb_product_info
            join tb_order_detail using (product_id)
            join tb_order_overall using (order_id)
        where
            datediff (
                (
                    select
                        max(event_time)
                    from
                        tb_order_overall
                ),
                event_time
            ) < 90
            and tag = '零食'
        group by
            product_id
    ) b using (product_id)
group by
    product_id
order by
    repurchase_rate desc,
    product_id
limit
    3

全部评论

相关推荐

苍蓝星上艾露:这简历。。。可以试试我写的开源简历优化工具https://github.com/weicanie/prisma-ai
点赞 评论 收藏
分享
有没有佬投这个呀,怎么样呀求问
投递中科院空天信息创新研究院等公司10个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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