知识点

  1. 窗口函数建立新表,主要是筛选出订单数量大于等于2的记录
  2. 左连接客户端表,使用ifnull函数,如果有客户端名称就取出现null值就是groupby,按照source进行分组,最后计数客户端出现次数

代码

select ifnull(c.name, 'GroupBuy') as source, 
count(*) as cnt
from (
    select *, count(*) over (partition by user_id) as r
    from order_info 
    where date > '2025-10-15'
    and product_name in ('C++', 'Python', 'Java')
    and status = 'completed'
    )  as t
left join client as c 
on c.id = t.client_id
where t.r >= 2
group by source
order by source