Skip to content

Commit 301be42

Browse files
authored
feat: add solutions to lc problem: No.1565 (doocs#3885)
No.1565.Unique Orders and Customers Per Month
1 parent b466c71 commit 301be42

File tree

4 files changed

+70
-8
lines changed

4 files changed

+70
-8
lines changed

solution/1500-1599/1565.Unique Orders and Customers Per Month/README.md

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,9 @@ Orders</code>
8282

8383
<!-- solution:start -->
8484

85-
### 方法一
85+
### 方法一:条件筛选 + 分组统计
86+
87+
我们可以先筛选出金额大于 $20$ 的订单,然后按月份进行分组统计订单数和顾客数。
8688

8789
<!-- tabs:start -->
8890

@@ -96,7 +98,28 @@ SELECT
9698
COUNT(DISTINCT customer_id) AS customer_count
9799
FROM Orders
98100
WHERE invoice > 20
99-
GROUP BY month;
101+
GROUP BY 1;
102+
```
103+
104+
#### Pandas
105+
106+
```python
107+
import pandas as pd
108+
109+
110+
def unique_orders_and_customers(orders: pd.DataFrame) -> pd.DataFrame:
111+
filtered_orders = orders[orders["invoice"] > 20]
112+
filtered_orders["month"] = (
113+
filtered_orders["order_date"].dt.to_period("M").astype(str)
114+
)
115+
result = (
116+
filtered_orders.groupby("month")
117+
.agg(
118+
order_count=("order_id", "count"), customer_count=("customer_id", "nunique")
119+
)
120+
.reset_index()
121+
)
122+
return result
100123
```
101124

102125
<!-- tabs:end -->

solution/1500-1599/1565.Unique Orders and Customers Per Month/README_EN.md

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ This table contains information about the orders made by customer_id.
4343
<p><strong class="example">Example 1:</strong></p>
4444

4545
<pre>
46-
<strong>Input:</strong>
46+
<strong>Input:</strong>
4747
Orders table:
4848
+----------+------------+-------------+------------+
4949
| order_id | order_date | customer_id | invoice |
@@ -59,7 +59,7 @@ Orders table:
5959
| 9 | 2021-01-07 | 3 | 31 |
6060
| 10 | 2021-01-15 | 2 | 20 |
6161
+----------+------------+-------------+------------+
62-
<strong>Output:</strong>
62+
<strong>Output:</strong>
6363
+---------+-------------+----------------+
6464
| month | order_count | customer_count |
6565
+---------+-------------+----------------+
@@ -68,7 +68,7 @@ Orders table:
6868
| 2020-12 | 2 | 1 |
6969
| 2021-01 | 1 | 1 |
7070
+---------+-------------+----------------+
71-
<strong>Explanation:</strong>
71+
<strong>Explanation:</strong>
7272
In September 2020 we have two orders from 2 different customers with invoices &gt; $20.
7373
In October 2020 we have two orders from 1 customer, and only one of the two orders has invoice &gt; $20.
7474
In November 2020 we have two orders from 2 different customers but invoices &lt; $20, so we don&#39;t include that month.
@@ -82,7 +82,9 @@ In January 2021 we have two orders from 2 different customers, but only one of t
8282

8383
<!-- solution:start -->
8484

85-
### Solution 1
85+
### Solution 1: Conditional Filtering + Grouping Statistics
86+
87+
We can first filter out orders with an amount greater than $20$, and then group by month to count the number of orders and customers.
8688

8789
<!-- tabs:start -->
8890

@@ -96,7 +98,28 @@ SELECT
9698
COUNT(DISTINCT customer_id) AS customer_count
9799
FROM Orders
98100
WHERE invoice > 20
99-
GROUP BY month;
101+
GROUP BY 1;
102+
```
103+
104+
#### Pandas
105+
106+
```python
107+
import pandas as pd
108+
109+
110+
def unique_orders_and_customers(orders: pd.DataFrame) -> pd.DataFrame:
111+
filtered_orders = orders[orders["invoice"] > 20]
112+
filtered_orders["month"] = (
113+
filtered_orders["order_date"].dt.to_period("M").astype(str)
114+
)
115+
result = (
116+
filtered_orders.groupby("month")
117+
.agg(
118+
order_count=("order_id", "count"), customer_count=("customer_id", "nunique")
119+
)
120+
.reset_index()
121+
)
122+
return result
100123
```
101124

102125
<!-- tabs:end -->
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import pandas as pd
2+
3+
4+
def unique_orders_and_customers(orders: pd.DataFrame) -> pd.DataFrame:
5+
filtered_orders = orders[orders["invoice"] > 20]
6+
filtered_orders["month"] = (
7+
filtered_orders["order_date"].dt.to_period("M").astype(str)
8+
)
9+
result = (
10+
filtered_orders.groupby("month")
11+
.agg(
12+
order_count=("order_id", "count"), customer_count=("customer_id", "nunique")
13+
)
14+
.reset_index()
15+
)
16+
return result

solution/1500-1599/1565.Unique Orders and Customers Per Month/Solution.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ SELECT
55
COUNT(DISTINCT customer_id) AS customer_count
66
FROM Orders
77
WHERE invoice > 20
8-
GROUP BY month;
8+
GROUP BY 1;

0 commit comments

Comments
 (0)