题解 | #牛客的课程订单分析(七)#
牛客的课程订单分析(七)
http://www.nowcoder.com/practice/d6f4a37f966145da8900ba9edcc4c068
我们首先知道题目的关键是查询出有两条购买记录以上的用户和根据客户端类别来统计用户使用这些客户端的数量,已知团购的client_id都为0则可用client_id来做统计分类
select client_id,is_group_buy, count(id) over(partition by user_id)as cnt,--用来筛选购买记录是两条及以上的字段 count(id) over(partition by client_id)as num --用来统计各个客户端用的使用数量 from order_info where date>'2025-10-15' and status='completed' and product_name in ('C++','Java','Python')
最后在将表连接成型
select (case when a.is_group_buy='Yes' then 'GroupBuy' else b.name end) as source, a.num from (select client_id,is_group_buy, count(id) over(partition by user_id)as cnt, count(id) over(partition by client_id)as num from order_info where date>'2025-10-15' and status='completed' and product_name in ('C++','Java','Python') )a left join client b on a.client_id=b.id where a.cnt>1 group by source order by source如果还有什么逻辑上不严谨的地方还请多多指出