题解 | #牛客的课程订单分析(五)#

牛客的课程订单分析(五)

https://www.nowcoder.com/practice/348afda488554ceb922efd2f3effc427

select
    a1.user_id,
    a1.first_buy_date,
    a2.second_buy_date,
    a3.cnt
from (
    select
        t1.user_id,
        t1.date first_buy_date
    from (
        select
            user_id,
            date,
            row_number() over(partition by user_id order by date) rn
        from order_info
        where date > '2025-10-15' and product_name in ('C++','Java','Python') and status = 'completed'
    ) t1
    where rn = 1
) a1
-- rn = 1 inner join rn = 2 :自然可以满足用户下单2个以及2个以上
inner join (
    select
        t1.user_id,
        t1.date second_buy_date
    from (
        select
            user_id,
            date,
            row_number() over(partition by user_id order by date) rn
        from order_info
        where date > '2025-10-15' and product_name in ('C++','Java','Python') and status = 'completed'
    ) t1
    where rn = 2
) a2
on a1.user_id = a2.user_id
inner join (
    -- 2025-10-15以后状态为购买成功的C++课程或Java课程或Python课程,各用户购买总次数
    select
        user_id,
        count(id) cnt
    from order_info
    where date > '2025-10-15' and product_name in ('C++','Java','Python') and status = 'completed'
    group by user_id
) a3
on a1.user_id = a3.user_id
order by user_id ;

全部评论

相关推荐

12-05 15:53
中南大学 Java
点赞 评论 收藏
分享
totoroyyw:千年老妖😂
投递华为等公司10个岗位
点赞 评论 收藏
分享
评论
1
收藏
分享
牛客网
牛客企业服务