列表

详情


SQL276. 牛客的课程订单分析(六)

描述

有很多同学在牛客购买课程来学习,购买会产生订单存到数据库里

有一个订单信息表(order_info),简况如下:

id user_id product_name status client_id date is_group_buy
1 557336 C++ no_completed 1 2025-10-10 No
2 230173543 Python completed
2 2025-10-12
No
3 57 JS completed
0 2025-10-23
Yes
4 57 C++
completed
3 2025-10-23
No
5 557336 Java completed
0 2025-10-23
Yes
6 57 Java
completed
1 2025-10-24
No
7 557336
C++
completed
0 2025-10-25
Yes

1行表示user_id557336的用户在2025-10-10的时候使用了client_id1的客户端下了C++课程的非拼团(is_group_buyNo)订单,但是状态为没有购买成功。

2行表示user_id230173543的用户在2025-10-12的时候使用了client_id2的客户端下了Python课程的非拼团(is_group_buyNo)订单,状态为购买成功。

。。。

最后1行表示user_id557336的用户在2025-10-25的时候使用了下了C++课程的拼团(is_group_buyYes)订单,拼团不统计客户端,所以client_id所以为0,状态为购买成功。

有一个客户端表(client)简况如下:

id name
1 PC
2 Android
3 IOS
4 H5

请你写出一个sql语句查询在2025-10-15以后,同一个用户下单2个以及2个以上状态为购买成功的C++课程或Java课程或Python课程的订单id是否拼团以及客户端名字信息,最后一列如果是非拼团订单,则显示对应客户端名字,如果是拼团订单,则显示NULL,并且按照order_infoid升序排序,以上例子查询结果如下:

id is_group_buy client_name
4 No IOS
5 Yes NULL
6 No
PC
7 Yes
NULL

解析:

id为46的订单满足以上条件,且因为4是通过IOS下单的非拼团订单输出对应的信息,6是通过PC下单的非拼团订单输出对应的信息以及客户端名字;

id为57的订单满足以上条件,且因为57都是拼团订单,输出对应的信息以及NULL;

按照id升序排序

示例1

输入:

drop table if exists order_info;
drop table if exists client;
CREATE TABLE order_info (
id int(4) NOT NULL,
user_id int(11) NOT NULL,
product_name varchar(256) NOT NULL,
status varchar(32) NOT NULL,
client_id int(4) NOT NULL,
date date NOT NULL,
is_group_buy varchar(32) NOT NULL,
PRIMARY KEY (id));

CREATE TABLE client(
id int(4) NOT NULL,
name varchar(32) NOT NULL,
PRIMARY KEY (id)
);

INSERT INTO order_info VALUES
(1,557336,'C++','no_completed',1,'2025-10-10','No'),
(2,230173543,'Python','completed',2,'2025-10-12','No'),
(3,57,'JS','completed',0,'2025-10-23','Yes'),
(4,57,'C++','completed',3,'2025-10-23','No'),
(5,557336,'Java','completed',0,'2025-10-23','Yes'),
(6,57,'Java','completed',1,'2025-10-24','No'),
(7,557336,'C++','completed',0,'2025-10-25','Yes');

INSERT INTO client VALUES
(1,'PC'),
(2,'Android'),
(3,'IOS'),
(4,'H5');

输出:

4|No|IOS
5|Yes|None
6|No|PC
7|Yes|None

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

Sqlite 解法, 执行用时: 10ms, 内存消耗: 3624KB, 提交时间: 2021-09-14

select oi.id,oi.is_group_buy,
case when oi.is_group_buy ='No' then c.name 
else 'None' end name
from order_info oi
left join client c on oi.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(user_id) >=2) 

Sqlite 解法, 执行用时: 11ms, 内存消耗: 3492KB, 提交时间: 2021-09-16

select a.id,a.is_group_buy,b.name from (
select * from order_info where user_id in (
select user_id from order_info where date>'2025-10-15' and status='completed' 
and (product_name='C++' or product_name='Java' or product_name='Python') 
group by user_id having count(1)>=2)  and date>'2025-10-15' and status='completed' 
and (product_name='C++' or product_name='Java' or product_name='Python'))a
left join client b on a.client_id=b.id

Sqlite 解法, 执行用时: 11ms, 内存消耗: 3512KB, 提交时间: 2021-09-12

with  new_order as 
(select 
o.*,
c.name as client_name
from order_info o 
left join client c on c.id = o.client_id
where date > '2025-10-15'
and product_name not in ('JS')
and status = 'completed')

select 
id,
is_group_buy,
case when is_group_buy = 'No'then client_name else NULL end as client_name
from new_order
where user_id in 
(select 
user_id 
from new_order 
group by user_id 
having count(user_id) >= 2)
order by id 

Sqlite 解法, 执行用时: 11ms, 内存消耗: 3576KB, 提交时间: 2021-12-06

select
    a.id,
    a.is_group_buy,
    ifnull(a.name,"None") as client_name
from(select
        *,
        count(*) over (partition by oi.user_id) as cnt
    from order_info oi
    left join client c on oi.client_id = c.id
    where oi.date > "2025-10-15"
    and oi.product_name in ("C++", "Python", "Java")
    and oi.status = "completed"
    ) as a
where a.cnt >= 2
order by a.id asc

Sqlite 解法, 执行用时: 11ms, 内存消耗: 3588KB, 提交时间: 2021-09-21

with t as (SELECT *, count(*) over (PARTITION BY user_id) as total
FROM order_info
where date > '2025-10-15' and (product_name = 'C++' or product_name = 'Java' or product_name = 'Python') and status = 'completed')

select t.id, t.is_group_buy, (case when t.is_group_buy = 'No' then c.name else NULL END) AS client_name
from t
left join 
client as c
on t.client_id = c.id
where total >= 2
order by t.id