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

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

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

“写出一个sql语句查询在2025-10-15以后,同一个用户下单2个以及2个以上状态为购买成功的C++课程或Java课程或Python课程的来源信息,第一列是显示的是客户端名字,如果是拼团订单则显示GroupBuy,第二列显示这个客户端(或者是拼团订单)有多少订单,最后结果按照第一列(source)升序排序”

重审题目,进行需求拆解:
1、根据条件中找出来源信息
2、在获得的来源信息中根据client_id分组计数
3、由于需要客户端名称显示,所以需要将2的结果与client表left join,并采用ifnull函数将null值设置为“GrouBuy”

1、根据条件需要找出来源信息,找出满足条件的user_id

select
    user_id
from order_info
where status = 'completed'
and product_name in ('C++', 'Java', 'Python')
and date > '2025-10-15'
group by user_id
having count(*) > 1

2、将选择的user_id作为条件,从原表中筛选出属于来源信息部分,并根据client_id分组计数

select
    client_id,
    count(*) as cnt
from order_info
where user_id in (
    select
        user_id
    from order_info
    where status = 'completed'
    and product_name in ('C++', 'Java', 'Python')
    and date > '2025-10-15'
    group by user_id
    having count(*) > 1
                      )
and status = 'completed'
and product_name in ('C++', 'Java', 'Python')
and date > '2025-10-15'
group by client_id

3、将2的结果与client表left join,并采用ifnull函数将null值设置为“GrouBuy”,即最终结果

select
    finull(c.name, 'GroupBuy') as source,
    t.cnt
from (
    select
        client_id,
        count(*) as cnt
    from order_info
    where user_id in (
        select
            user_id
        from order_info
        where status = 'completed'
        and product_name in ('C++', 'Java', 'Python')
        and date > '2025-10-15'
        group by user_id
        having count(*) > 1
                      )
    and status = 'completed'
    and product_name in ('C++', 'Java', 'Python')
    and date > '2025-10-15'
    group by client_id
      ) as t
left join client as c
on t.client_id = c.id
order by source
全部评论
小细节:finull --> ifnull
6 回复 分享
发布于 2022-03-28 09:35

相关推荐

敢逐云霄志:你打招呼语怎么能这么长,hr都没看下去的欲望,简明扼要说重点,就读于某某学校某某专业,26届应届毕业生,学信网可查,先后在某某公司实习过(如有),然后做过什么项目,想找一份什么样的工作,可实习几个月以上,期待您的回复。
点赞 评论 收藏
分享
评论
39
4
分享

创作者周榜

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