1565. 按月统计订单数与顾客数

表:Orders

+---------------+---------+
| Column Name | Type |
+---------------+---------+
| order_id | int |
| order_date | date |
| customer_id | int |
| invoice | int |
+---------------+---------+
order_id 是 Orders 表的主键。
这张表包含顾客(customer_id)所下订单的信息。
写一个查询语句来 按月 统计 金额大于 $20 的唯一 订单数 和唯一 顾客数 。

查询结果无排序要求。

解题方法:

select
date_format(order_date,'%Y-%m') as 'month',
count(distinct(order_id)) order_count,
count(distinct(customer_id)) customer_count
from orders
where invoice>20
group by date_format(order_date,'%Y-%m')
备注:需要理解mysql执行及优化逻辑
原文地址:https://www.cnblogs.com/tomorrow-hope/p/13834385.html