Skip to content
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
41 changes: 40 additions & 1 deletion solution/3600-3699/3657.Find Loyal Customers/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ tags:

<pre>
+------------------+---------+
| Column Name | Type |
| Column Name | Type |
+------------------+---------+
| transaction_id | int |
| customer_id | int |
Expand Down Expand Up @@ -146,7 +146,46 @@ transaction_type can be either &#39;purchase&#39; or &#39;refund&#39;.
#### MySQL

```sql
# Write your MySQL query statement below
SELECT customer_id
FROM customer_transactions
GROUP BY 1
HAVING
COUNT(1) >= 3
AND SUM(transaction_type = 'refund') / COUNT(1) < 0.2
AND DATEDIFF(MAX(transaction_date), MIN(transaction_date)) >= 30
ORDER BY 1;
```

#### Pandas

```python
import pandas as pd


def find_loyal_customers(customer_transactions: pd.DataFrame) -> pd.DataFrame:
customer_transactions["transaction_date"] = pd.to_datetime(
customer_transactions["transaction_date"]
)
grouped = customer_transactions.groupby("customer_id")
agg_df = grouped.agg(
total_transactions=("transaction_type", "size"),
refund_count=("transaction_type", lambda x: (x == "refund").sum()),
min_date=("transaction_date", "min"),
max_date=("transaction_date", "max"),
).reset_index()
agg_df["date_diff"] = (agg_df["max_date"] - agg_df["min_date"]).dt.days
agg_df["refund_ratio"] = agg_df["refund_count"] / agg_df["total_transactions"]
result = (
agg_df[
(agg_df["total_transactions"] >= 3)
& (agg_df["refund_ratio"] < 0.2)
& (agg_df["date_diff"] >= 30)
][["customer_id"]]
.sort_values("customer_id")
.reset_index(drop=True)
)
return result
```

<!-- tabs:end -->
Expand Down
41 changes: 40 additions & 1 deletion solution/3600-3699/3657.Find Loyal Customers/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ tags:

<pre>
+------------------+---------+
| Column Name | Type |
| Column Name | Type |
+------------------+---------+
| transaction_id | int |
| customer_id | int |
Expand Down Expand Up @@ -146,7 +146,46 @@ transaction_type can be either &#39;purchase&#39; or &#39;refund&#39;.
#### MySQL

```sql
# Write your MySQL query statement below
SELECT customer_id
FROM customer_transactions
GROUP BY 1
HAVING
COUNT(1) >= 3
AND SUM(transaction_type = 'refund') / COUNT(1) < 0.2
AND DATEDIFF(MAX(transaction_date), MIN(transaction_date)) >= 30
ORDER BY 1;
```

#### Pandas

```python
import pandas as pd


def find_loyal_customers(customer_transactions: pd.DataFrame) -> pd.DataFrame:
customer_transactions["transaction_date"] = pd.to_datetime(
customer_transactions["transaction_date"]
)
grouped = customer_transactions.groupby("customer_id")
agg_df = grouped.agg(
total_transactions=("transaction_type", "size"),
refund_count=("transaction_type", lambda x: (x == "refund").sum()),
min_date=("transaction_date", "min"),
max_date=("transaction_date", "max"),
).reset_index()
agg_df["date_diff"] = (agg_df["max_date"] - agg_df["min_date"]).dt.days
agg_df["refund_ratio"] = agg_df["refund_count"] / agg_df["total_transactions"]
result = (
agg_df[
(agg_df["total_transactions"] >= 3)
& (agg_df["refund_ratio"] < 0.2)
& (agg_df["date_diff"] >= 30)
][["customer_id"]]
.sort_values("customer_id")
.reset_index(drop=True)
)
return result
```

<!-- tabs:end -->
Expand Down
26 changes: 26 additions & 0 deletions solution/3600-3699/3657.Find Loyal Customers/Solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import pandas as pd


def find_loyal_customers(customer_transactions: pd.DataFrame) -> pd.DataFrame:
customer_transactions["transaction_date"] = pd.to_datetime(
customer_transactions["transaction_date"]
)
grouped = customer_transactions.groupby("customer_id")
agg_df = grouped.agg(
total_transactions=("transaction_type", "size"),
refund_count=("transaction_type", lambda x: (x == "refund").sum()),
min_date=("transaction_date", "min"),
max_date=("transaction_date", "max"),
).reset_index()
agg_df["date_diff"] = (agg_df["max_date"] - agg_df["min_date"]).dt.days
agg_df["refund_ratio"] = agg_df["refund_count"] / agg_df["total_transactions"]
result = (
agg_df[
(agg_df["total_transactions"] >= 3)
& (agg_df["refund_ratio"] < 0.2)
& (agg_df["date_diff"] >= 30)
][["customer_id"]]
.sort_values("customer_id")
.reset_index(drop=True)
)
return result
9 changes: 9 additions & 0 deletions solution/3600-3699/3657.Find Loyal Customers/Solution.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Write your MySQL query statement below
SELECT customer_id
FROM customer_transactions
GROUP BY 1
HAVING
COUNT(1) >= 3
AND SUM(transaction_type = 'refund') / COUNT(1) < 0.2
AND DATEDIFF(MAX(transaction_date), MIN(transaction_date)) >= 30
ORDER BY 1;