with satisfied_order as ( select (case client_id when 0 then 'GroupBuy' else name end) as source, count(*) over(partition by user_id) as order_cnt from order_info o left join client c on o.client_id = c.id where date > '2025-10-15' and status = 'completed' and product_name in ('C++','Java','Python') ) select source, count(*) as cnt from satisfied_order where order_cnt >= 2 group by source order by source
- 统计符合除了下单次数>=2的其他条件的订单数据,连接client表获取name。对name做判断,当client_id=0说明为团购订单,赋值为GroupBuy;其余都可以得到name值,使用name值。最后记录订单对应的用户的下单次数。
- 筛选出下单次数>=2的订单数据,分组按source计数。完成题解。
注意:1. group by '字段名'会直接对数据进行分组合并计数,会影响数据行数,'字段名'是unique的;2.count() over(partition by)窗口函数不会影响原数据,会保留每一行的数据,'字段名'仍像原来一样,不合并,只是在最后加上分区统计结果值。