Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add solutions to lc problem: No.1565 #3885

Merged
merged 1 commit into from
Dec 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
feat: add solutions to lc problem: No.1565
No.1565.Unique Orders and Customers Per Month
  • Loading branch information
yanglbme committed Dec 25, 2024
commit 381375095cc8f2789c6454acc68edfe936ef7ec6
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@ Orders</code>

<!-- solution:start -->

### 方法一
### 方法一:条件筛选 + 分组统计

我们可以先筛选出金额大于 $20$ 的订单,然后按月份进行分组统计订单数和顾客数。

<!-- tabs:start -->

Expand All @@ -96,7 +98,28 @@ SELECT
COUNT(DISTINCT customer_id) AS customer_count
FROM Orders
WHERE invoice > 20
GROUP BY month;
GROUP BY 1;
```

#### Pandas

```python
import pandas as pd


def unique_orders_and_customers(orders: pd.DataFrame) -> pd.DataFrame:
filtered_orders = orders[orders["invoice"] > 20]
filtered_orders["month"] = (
filtered_orders["order_date"].dt.to_period("M").astype(str)
)
result = (
filtered_orders.groupby("month")
.agg(
order_count=("order_id", "count"), customer_count=("customer_id", "nunique")
)
.reset_index()
)
return result
```

<!-- tabs:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ This table contains information about the orders made by customer_id.
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong>
<strong>Input:</strong>
Orders table:
+----------+------------+-------------+------------+
| order_id | order_date | customer_id | invoice |
Expand All @@ -59,7 +59,7 @@ Orders table:
| 9 | 2021-01-07 | 3 | 31 |
| 10 | 2021-01-15 | 2 | 20 |
+----------+------------+-------------+------------+
<strong>Output:</strong>
<strong>Output:</strong>
+---------+-------------+----------------+
| month | order_count | customer_count |
+---------+-------------+----------------+
Expand All @@ -68,7 +68,7 @@ Orders table:
| 2020-12 | 2 | 1 |
| 2021-01 | 1 | 1 |
+---------+-------------+----------------+
<strong>Explanation:</strong>
<strong>Explanation:</strong>
In September 2020 we have two orders from 2 different customers with invoices &gt; $20.
In October 2020 we have two orders from 1 customer, and only one of the two orders has invoice &gt; $20.
In November 2020 we have two orders from 2 different customers but invoices &lt; $20, so we don&#39;t include that month.
Expand All @@ -82,7 +82,9 @@ In January 2021 we have two orders from 2 different customers, but only one of t

<!-- solution:start -->

### Solution 1
### Solution 1: Conditional Filtering + Grouping Statistics

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.

<!-- tabs:start -->

Expand All @@ -96,7 +98,28 @@ SELECT
COUNT(DISTINCT customer_id) AS customer_count
FROM Orders
WHERE invoice > 20
GROUP BY month;
GROUP BY 1;
```

#### Pandas

```python
import pandas as pd


def unique_orders_and_customers(orders: pd.DataFrame) -> pd.DataFrame:
filtered_orders = orders[orders["invoice"] > 20]
filtered_orders["month"] = (
filtered_orders["order_date"].dt.to_period("M").astype(str)
)
result = (
filtered_orders.groupby("month")
.agg(
order_count=("order_id", "count"), customer_count=("customer_id", "nunique")
)
.reset_index()
)
return result
```

<!-- tabs:end -->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import pandas as pd


def unique_orders_and_customers(orders: pd.DataFrame) -> pd.DataFrame:
filtered_orders = orders[orders["invoice"] > 20]
filtered_orders["month"] = (
filtered_orders["order_date"].dt.to_period("M").astype(str)
)
result = (
filtered_orders.groupby("month")
.agg(
order_count=("order_id", "count"), customer_count=("customer_id", "nunique")
)
.reset_index()
)
return result
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ SELECT
COUNT(DISTINCT customer_id) AS customer_count
FROM Orders
WHERE invoice > 20
GROUP BY month;
GROUP BY 1;
Loading