题解 | #牛客的课程订单分析(六)#
牛客的课程订单分析(六)
https://www.nowcoder.com/practice/c5736983c322483e9f269dd23bdf2f6f
# 请你写出一个sql语句查询在2025-10-15以后,同一个用户下单2个以及2个以上状态为购买成功的C++课程或Java课程或Python课程的订单id,是否拼团以及客户端名字信息,最后一列如果是非拼团订单,则显示对应客户端名字,如果是拼团订单,则显示NULL,并且按照order_info的id升序排序 # 方法:开窗函数 # 表连接时需要使用left join来左连结order_info表,因为client_id = 0这个数据在client表里是不存在的,会在连结时被去掉 # 先建立一个表t # select o.id,o.is_group_buy,c.name as client_name ,count(*)over(partition by o.user_id ) as cnt # from order_info o left join client c # on o.client_id=c.id # where datediff(date,"2025-10-15")>0 and o.product_name in ('C++', 'Java', 'Python') and o.status='completed'; select t.id,t.is_group_buy,t.client_name from (select o.id,o.is_group_buy,c.name as client_name ,count(*)over(partition by o.user_id ) as cnt from order_info o left join client c on o.client_id=c.id where datediff(date,"2025-10-15")>0 and o.product_name in ('C++', 'Java', 'Python') and o.status='completed') t where t.cnt>=2 order by t.id;