列表

详情


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

描述

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

有一个订单信息表(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课程的来源信息,第一列是显示的是客户端名字,如果是拼团订单则显示GroupBuy第二列显示这个客户端(或者是拼团订单)有多少订单,最后结果按照第一列(source)升序排序,以上例子查询结果如下:

source cnt
GroupBuy 2
IOS 1
PC 1

解析:

id为46的订单满足以上条件,且因为4是通过IOS下单的非拼团订单则记: IOS 1

6是通过PC下单的非拼团订单则记: PC 1;

id为57的订单满足以上条件,且因为57都是拼团订单,则记: GroupBuy 2;

最后按照source升序排序。

示例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');

输出:

GroupBuy|2
IOS|1
PC|1

原站题解

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

Sqlite 解法, 执行用时: 10ms, 内存消耗: 3504KB, 提交时间: 2021-12-01

select 
    source,
    count(id) as cnt 
from 
    (
    select 
        user_id,
        table1.id,
        name,
        case when is_group_buy = 'Yes' then 'GroupBuy' else name end as source
    from 
        (
        select 
            id,
            user_id,
            client_id,
            is_group_buy,
            count(id)over(partition by user_id) as cnt
        from order_info
        where date > '2025-10-15'
        and status = 'completed'
        and product_name in ('C++','Java','Python')
        ) table1
    left join client table2 
    on table1.client_id = table2.id
    where cnt >= 2 
    ) table3
group by source

Sqlite 解法, 执行用时: 10ms, 内存消耗: 3504KB, 提交时间: 2021-11-30

SELECT source, COUNT(*) AS cnt
FROM (SELECT oi.id, oi.user_id, oi.is_group_buy, 
       (CASE WHEN oi.client_id=0
            THEN 'GroupBuy'
            ELSE c.name
        END) AS source, 
        COUNT(*) OVER(PARTITION BY oi.user_id) AS tcnt
        FROM order_info oi LEFT JOIN client c ON c.id=oi.client_id
        WHERE date>'2025-10-15'
        AND status='completed'
        AND product_name IN ('C++','Java','Python')) t1
WHERE t1.tcnt>=2
GROUP BY source
ORDER BY source;

Sqlite 解法, 执行用时: 10ms, 内存消耗: 3516KB, 提交时间: 2022-01-25

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

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

select coalesce(c.name,'GroupBuy') as source,count(o.client_id) from order_info o left join client c 
on o.client_id=c.id where (o.product_name='C++' or o.product_name='Java' or o.product_name='Python')
and o.status='completed' and o.date>'2025-10-15'
and o.user_id in (select o1.user_id from order_info o1
where (o1.product_name='C++' or o1.product_name='Java' or o1.product_name='Python')
and o1.status='completed' and o1.date>'2025-10-15' group by o1.user_id having count(o1.status)>1) 
group by o.client_id order by source;


Sqlite 解法, 执行用时: 10ms, 内存消耗: 3784KB, 提交时间: 2021-08-07

select (case when is_group_buy='Yes' then 'GroupBuy' else name end) as source,count(is_group_buy) as cnt from 
(select table_a.*,client.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 in('C++','Java','Python')
group by user_id having count(user_id)>1) and date>'2025-10-15' and status='completed' and product_name in('C++','Java','Python'))
table_a  
left join client
on table_a.client_id=client.id
)first_filter

group by client_id,is_group_buy
order by source