|
| 1 | +Here's a well-structured `README.md` file for **LeetCode 1193 - Monthly Transactions I**, formatted for a GitHub repository: |
| 2 | + |
| 3 | +```md |
| 4 | +# 🏦 Monthly Transactions I - LeetCode 1193 |
| 5 | + |
| 6 | +## 📌 Problem Statement |
| 7 | +You are given the **Transactions** table that records financial transactions. |
| 8 | + |
| 9 | +### Transactions Table |
| 10 | +| Column Name | Type | |
| 11 | +| ----------- | ------- | |
| 12 | +| id | int | |
| 13 | +| country | varchar | |
| 14 | +| state | enum | |
| 15 | +| amount | int | |
| 16 | +| trans_date | date | |
| 17 | + |
| 18 | +- **id** is the **primary key**. |
| 19 | +- The **state** column is an `ENUM` type with values **"approved"** and **"declined"**. |
| 20 | +- Each row **records a transaction** with an amount and a transaction date. |
| 21 | + |
| 22 | +### Task: |
| 23 | +Find **monthly statistics** for each country: |
| 24 | +- Total **number of transactions**. |
| 25 | +- Total **amount of transactions**. |
| 26 | +- Total **number of approved transactions**. |
| 27 | +- Total **amount of approved transactions**. |
| 28 | + |
| 29 | +The **month format should be `YYYY-MM`**. |
| 30 | + |
| 31 | +--- |
| 32 | + |
| 33 | +## 📊 Example 1: |
| 34 | +### Input: |
| 35 | +**Transactions Table** |
| 36 | +| id | country | state | amount | trans_date | |
| 37 | +| --- | ------- | -------- | ------ | ---------- | |
| 38 | +| 121 | US | approved | 1000 | 2018年12月18日 | |
| 39 | +| 122 | US | declined | 2000 | 2018年12月19日 | |
| 40 | +| 123 | US | approved | 2000 | 2019年01月01日 | |
| 41 | +| 124 | DE | approved | 2000 | 2019年01月07日 | |
| 42 | + |
| 43 | +### Output: |
| 44 | +| month | country | trans_count | approved_count | trans_total_amount | approved_total_amount | |
| 45 | +| ------- | ------- | ----------- | -------------- | ------------------ | --------------------- | |
| 46 | +| 2018-12 | US | 2 | 1 | 3000 | 1000 | |
| 47 | +| 2019-01 | US | 1 | 1 | 2000 | 2000 | |
| 48 | +| 2019-01 | DE | 1 | 1 | 2000 | 2000 | |
| 49 | + |
| 50 | +### Explanation: |
| 51 | +- **December 2018 (US)**: |
| 52 | + - **2 transactions** (1000 + 2000). |
| 53 | + - **1 approved transaction** (1000). |
| 54 | +- **January 2019 (US)**: |
| 55 | + - **1 transaction** (2000). |
| 56 | + - **1 approved transaction** (2000). |
| 57 | +- **January 2019 (DE)**: |
| 58 | + - **1 transaction** (2000). |
| 59 | + - **1 approved transaction** (2000). |
| 60 | + |
| 61 | +--- |
| 62 | + |
| 63 | +## 🖥 SQL Solution |
| 64 | + |
| 65 | +### 1️⃣ Standard MySQL Solution |
| 66 | +#### Explanation: |
| 67 | +- **Extract the month** from `trans_date` using `DATE_FORMAT()`. |
| 68 | +- **Count transactions** for each `month` and `country`. |
| 69 | +- **Sum transaction amounts**. |
| 70 | +- **Filter only approved transactions** separately using `CASE WHEN`. |
| 71 | + |
| 72 | +```sql |
| 73 | +SELECT |
| 74 | + DATE_FORMAT(trans_date, '%Y-%m') AS month, |
| 75 | + country, |
| 76 | + COUNT(id) AS trans_count, |
| 77 | + SUM(CASE WHEN state = 'approved' THEN 1 ELSE 0 END) AS approved_count, |
| 78 | + SUM(amount) AS trans_total_amount, |
| 79 | + SUM(CASE WHEN state = 'approved' THEN amount ELSE 0 END) AS approved_total_amount |
| 80 | +FROM Transactions |
| 81 | +GROUP BY month, country |
| 82 | +ORDER BY month, country; |
| 83 | +``` |
| 84 | + |
| 85 | +--- |
| 86 | + |
| 87 | +## 🐍 Pandas Solution (Python) |
| 88 | +#### Explanation: |
| 89 | +- **Extract the month (`YYYY-MM`)** from `trans_date`. |
| 90 | +- **Group by month and country**. |
| 91 | +- **Compute counts and sums** using `.agg()`. |
| 92 | + |
| 93 | +```python |
| 94 | +import pandas as pd |
| 95 | + |
| 96 | +def monthly_transactions(transactions: pd.DataFrame) -> pd.DataFrame: |
| 97 | + # Extract 'YYYY-MM' from the trans_date |
| 98 | + transactions['month'] = transactions['trans_date'].dt.strftime('%Y-%m') |
| 99 | + |
| 100 | + # Aggregate transaction counts and sums |
| 101 | + result = transactions.groupby(['month', 'country']).agg( |
| 102 | + trans_count=('id', 'count'), |
| 103 | + approved_count=('state', lambda x: (x == 'approved').sum()), |
| 104 | + trans_total_amount=('amount', 'sum'), |
| 105 | + approved_total_amount=('amount', lambda x: x[transactions['state'] == 'approved'].sum()) |
| 106 | + ).reset_index() |
| 107 | + |
| 108 | + return result.sort_values(['month', 'country']) |
| 109 | +``` |
| 110 | + |
| 111 | +--- |
| 112 | + |
| 113 | +## 📁 File Structure |
| 114 | +``` |
| 115 | +📂 Monthly-Transactions |
| 116 | +│── 📜 README.md |
| 117 | +│── 📜 solution.sql |
| 118 | +│── 📜 solution_pandas.py |
| 119 | +│── 📜 test_cases.sql |
| 120 | +``` |
| 121 | + |
| 122 | +--- |
| 123 | + |
| 124 | +## 🔗 Useful Links |
| 125 | +- 📖 [LeetCode Problem](https://leetcode.com/problems/monthly-transactions-i/) |
| 126 | +- 📚 [SQL `GROUP BY` Clause](https://www.w3schools.com/sql/sql_groupby.asp) |
| 127 | +- 🐍 [Pandas GroupBy Documentation](https://pandas.pydata.org/docs/reference/groupby.html) |
| 128 | +``` |
| 129 | + |
| 130 | +This README includes: |
| 131 | +- **Problem statement** |
| 132 | +- **Example input and output** |
| 133 | +- **SQL solution with explanations** |
| 134 | +- **Pandas solution in Python** |
| 135 | +- **File structure for GitHub** |
| 136 | +- **Useful links** |
| 137 | + |
| 138 | +Let me know if you need any modifications! 🚀 |
0 commit comments