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

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

http://www.nowcoder.com/practice/d6f4a37f966145da8900ba9edcc4c068

思路:在上一题中的使用case的题解中再增加一个分组和count即可。

case也可以被 ifnull(c.name, 'GroupBuy') 函数替换。

完整代码:

select 
	case when o.is_group_buy = 'Yes' then 'GroupBuy'
	when o.is_group_buy='No' then name end as source,
	count(*) as 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')
and user_id IN
(
    select user_id
    from order_info
    where date > '2025-10-15'
    and status = 'completed'
    and product_name IN ('C++','Java','Python')
    group by user_id
    having count(status) > 1
)
group by source
order by source

使用ifnull函数:

select 
	ifnull(c.name, 'GroupBuy') as source,
	count(*) as 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')
and user_id IN
(
    select user_id
    from order_info
    where date > '2025-10-15'
    and status = 'completed'
    and product_name IN ('C++','Java','Python')
    group by user_id
    having count(status) > 1
)
group by source
order by source
SQL练习 文章被收录于专栏

已完成牛客的SQL练习。接下来是算法的练习

全部评论

相关推荐

宇算唯航:目测实缴资本不超100W的小公司
点赞 评论 收藏
分享
07-07 14:30
复旦大学 Java
遇到这种人我也不知道说啥了
无能的丈夫:但我觉得这个hr语气没什么问题啊(没有恶意
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-08 13:15
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务