147,SQL训练之,力扣,1565. 按月统计订单数与顾客数

  • 学习:知识的初次邂逅
  • 复习:知识的温故知新
  • 练习:知识的实践应用

目录

一,原题力扣链接

二,题干

三,建表语句

四,分析

五,SQL解答

六,验证

七,知识点总结


一,原题力扣链接

. - 力扣(LeetCode)

二,题干

表:Orders

+---------------+---------+
| Column Name   | Type    |
+---------------+---------+
| order_id      | int     |
| order_date    | date    |
| customer_id   | int     |
| invoice       | int     |
+---------------+---------+
order_id 是该表具有唯一值的列。
这张表包含顾客(customer_id)所下订单的信息。

写一个查询语句来 按月 统计金额(invoice)大于 $20 的唯一 订单数 和唯一 顾客数 。

查询结果无排序要求。

查询结果格式如下所示。

示例 1:

输入:
Orders
+----------+------------+-------------+------------+
| order_id | order_date | customer_id | invoice    |
+----------+------------+-------------+------------+
| 1        | 2020-09-15 | 1           | 30         |
| 2        | 2020-09-17 | 2           | 90         |
| 3        | 2020-10-06 | 3           | 20         |
| 4        | 2020-10-20 | 3           | 21         |
| 5        | 2020-11-10 | 1           | 10         |
| 6        | 2020-11-21 | 2           | 15         |
| 7        | 2020-12-01 | 4           | 55         |
| 8        | 2020-12-03 | 4           | 77         |
| 9        | 2021-01-07 | 3           | 31         |
| 10       | 2021-01-15 | 2           | 20         |
+----------+------------+-------------+------------+
输出:
+---------+-------------+----------------+
| month   | order_count | customer_count |
+---------+-------------+----------------+
| 2020-09 | 2           | 2              |
| 2020-10 | 1           | 1              |
| 2020-12 | 2           | 1              |
| 2021-01 | 1           | 1              |
+---------+-------------+----------------+
解释:
在 2020 年 09 月,有 2 份来自 2 位不同顾客的金额大于 $20 的订单。
在 2020 年 10 月,有 2 份来自 1 位顾客的订单,并且只有其中的 1 份订单金额大于 $20 。
在 2020 年 11 月,有 2 份来自 2 位不同顾客的订单,但由于金额都小于 $20 ,所以我们的查询结果中不包含这个月的数据。
在 2020 年 12 月,有 2 份来自 1 位顾客的订单,且 2 份订单金额都大于 $20 。
在 2021 年 01 月,有 2 份来自 2 位不同顾客的订单,但只有其中一份订单金额大于 $20 。

三,建表语句


Create table If Not Exists Orders (order_id int, order_date date, customer_id int, invoice int);
Truncate table Orders;
insert into Orders (order_id, order_date, customer_id, invoice) values ('1', '2020-09-15', '1', '30');
insert into Orders (order_id, order_date, customer_id, invoice) values ('2', '2020-09-17', '2', '90');
insert into Orders (order_id, order_date, customer_id, invoice) values ('3', '2020-10-06', '3', '20');
insert into Orders (order_id, order_date, customer_id, invoice) values ('4', '2020-10-20', '3', '21');
insert into Orders (order_id, order_date, customer_id, invoice) values ('5', '2020-11-10', '1', '10');;
insert into Orders (order_id, order_date, customer_id, invoice) values ('6', '2020-11-21', '2', '15');
insert into Orders (order_id, order_date, customer_id, invoice) values ('7', '2020-12-01', '4', '55');
insert into Orders (order_id, order_date, customer_id, invoice) values ('8', '2020-12-03', '4', '77');
insert into Orders (order_id, order_date, customer_id, invoice) values ('9', '2021-01-07', '3', '31');
insert into Orders (order_id, order_date, customer_id, invoice) values ('10', '2021-01-15', '2', '20');


select * from Orders;

四,分析

思路

表格大法
第一步:转换订单时间 转为 年+月 并且只留下大于20的行
第二步:以转换的时间分组 count 订单函数,count 客户id(客户id可能有重复,所以需要distinct去重一下)
第三步: 映射指定的列,改名,输出

解题过程

pandas+Mysql实现上述表格逻辑

第一步:转换订单时间 转为 年+月 并且只留下大于20的行

在mysql

第二步:以转换的时间分组 count 订单函数,count 客户id(客户id可能有重复,所以需要distinct去重一下)

在mysql

第三步: 映射指定的列,改名,输出

在mysql

五,SQL解答

select
    substring(order_date,1,7) as month
     , count(order_id) as order_count
     ,count(distinct customer_id) as customer_count
    from orders where invoice>20 group by substring(order_date,1,7);

六,验证

七,知识点总结

  • 时间类型的截取
  • 分组聚合的运用
  • 分组聚合+去重的运用

  • 学习:知识的初次邂逅
  • 复习:知识的温故知新
  • 练习:知识的实践应用
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值