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

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

https://www.nowcoder.com/practice/c5736983c322483e9f269dd23bdf2f6f

select a.id
     ,is_group_buy
     ,name as client_name
from (
    select id
         ,is_group_buy
         ,product_name
         ,client_id
         ,count(1)over(partition by user_id) as cnt
    from order_info
    where date>'2025-10-15'
      and status='completed'
      and product_name in ('C++','Java','Python')
    ) a
    left join client
    on a.client_id=client.id
where cnt>=2
order by a.id;

关键点在于同一个用户下单2个以及2个以上。。。第一反应会用group by ,但是使用了group by来判定次数,会导致id,is_group_buy ,product_name,client_id 取不到

问题就转变为有没有什么能不用group by但是也能统计出购买次数的呢,答案就是窗口函数,用count()over() 就能达成!

然后 左连接client ,连接键 client.id ,连不上的为null,最后取出所需列即可

全部评论

相关推荐

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

创作者周榜

更多
牛客网
牛客企业服务