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

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

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

select 
    t1.user_id,
    t1.date first_buy_date,
    t2.date second_buy_date,
    t3.cnt
from
    (select
        user_id,
        min(date) date
    from 
        order_info
    where
        status = 'completed'
        and product_name in ('C++','Python','Java')
        and date > '2025-10-15'
    group by
        user_id
    having
        count(*) > 1) t1
left join
    (select 
        user_id,
        date
    from 
        (select
            user_id,
            date,
            row_number() over(partition by user_id order by date) rn
        from 
            order_info
        where
            status = 'completed'
            and product_name in ('C++','Python','Java')
            and date > '2025-10-15'
        ) t
    where
        rn = 2) t2
on
    t1.user_id = t2.user_id
left join
    (select
        user_id,
        count(*) cnt
    from
        order_info
    where
        status = 'completed'
        and product_name in ('C++','Python','Java')
        and date > '2025-10-15'
    group by
        user_id
    having 
        count(*) > 1) t3
on
    t1.user_id = t3.user_id
order by 
    t1.user_id

全部评论

相关推荐

点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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