| 
 | 1 | +Here’s a well-structured `README.md` file for **LeetCode 1141 - User Activity for the Past 30 Days I** with SQL and Pandas solutions:  | 
 | 2 | + | 
 | 3 | +```md  | 
 | 4 | +# 📊 User Activity for the Past 30 Days I - LeetCode 1141  | 
 | 5 | + | 
 | 6 | +## 📌 Problem Statement  | 
 | 7 | +You are given the **Activity** table that records user activities on a social media website.  | 
 | 8 | + | 
 | 9 | +### Activity Table  | 
 | 10 | +| Column Name | Type |  | 
 | 11 | +| ------------- | ---- |  | 
 | 12 | +| user_id | int |  | 
 | 13 | +| session_id | int |  | 
 | 14 | +| activity_date | date |  | 
 | 15 | +| activity_type | enum |  | 
 | 16 | + | 
 | 17 | +- The `activity_type` column is an ENUM of **('open_session', 'end_session', 'scroll_down', 'send_message')**.  | 
 | 18 | +- Each session belongs to exactly **one user**.  | 
 | 19 | +- The table **may have duplicate rows**.  | 
 | 20 | + | 
 | 21 | +### Task:  | 
 | 22 | +Find the **daily active user count** for a period of **30 days ending 2019-07-27 inclusively**.  | 
 | 23 | +- A user is considered **active on a given day** if they made at least **one activity**.  | 
 | 24 | +- Ignore days with **zero active users**.  | 
 | 25 | + | 
 | 26 | +---  | 
 | 27 | + | 
 | 28 | +## 📊 Example 1:  | 
 | 29 | +### Input:  | 
 | 30 | +**Activity Table**  | 
 | 31 | +| user_id | session_id | activity_date | activity_type |  | 
 | 32 | +| ------- | ---------- | ------------- | ------------- |  | 
 | 33 | +| 1 | 1 | 2019-07-20 | open_session |  | 
 | 34 | +| 1 | 1 | 2019-07-20 | scroll_down |  | 
 | 35 | +| 1 | 1 | 2019-07-20 | end_session |  | 
 | 36 | +| 2 | 4 | 2019-07-20 | open_session |  | 
 | 37 | +| 2 | 4 | 2019-07-21 | send_message |  | 
 | 38 | +| 2 | 4 | 2019-07-21 | end_session |  | 
 | 39 | +| 3 | 2 | 2019-07-21 | open_session |  | 
 | 40 | +| 3 | 2 | 2019-07-21 | send_message |  | 
 | 41 | +| 3 | 2 | 2019-07-21 | end_session |  | 
 | 42 | +| 4 | 3 | 2019-06-25 | open_session |  | 
 | 43 | +| 4 | 3 | 2019-06-25 | end_session |  | 
 | 44 | + | 
 | 45 | +### Output:  | 
 | 46 | +| day | active_users |  | 
 | 47 | +| ---------- | ------------ |  | 
 | 48 | +| 2019-07-20 | 2 |  | 
 | 49 | +| 2019-07-21 | 2 |  | 
 | 50 | + | 
 | 51 | +### Explanation:  | 
 | 52 | +- **2019-07-20**: Users **1 and 2** were active.  | 
 | 53 | +- **2019-07-21**: Users **2 and 3** were active.  | 
 | 54 | +- **Days with zero active users are ignored**.  | 
 | 55 | + | 
 | 56 | +---  | 
 | 57 | + | 
 | 58 | +## 🖥 SQL Solutions  | 
 | 59 | + | 
 | 60 | +### 1️⃣ Standard MySQL Solution  | 
 | 61 | +#### Explanation:  | 
 | 62 | +- **Filter records** for the last **30 days** (ending on `2019-07-27`).  | 
 | 63 | +- Use `COUNT(DISTINCT user_id)` to count **unique active users per day**.  | 
 | 64 | +- Ignore **days with zero active users**.  | 
 | 65 | + | 
 | 66 | +```sql  | 
 | 67 | +SELECT   | 
 | 68 | + activity_date AS day,   | 
 | 69 | + COUNT(DISTINCT user_id) AS active_users  | 
 | 70 | +FROM   | 
 | 71 | + Activity  | 
 | 72 | +WHERE   | 
 | 73 | + DATEDIFF('2019-07-27', activity_date) < 30   | 
 | 74 | + AND DATEDIFF('2019-07-27', activity_date) >= 0  | 
 | 75 | +GROUP BY activity_date;  | 
 | 76 | +```  | 
 | 77 | + | 
 | 78 | +---  | 
 | 79 | + | 
 | 80 | +### 2️⃣ Alternative Solution Using `BETWEEN`  | 
 | 81 | +#### Explanation:  | 
 | 82 | +- This solution filters the date range using `BETWEEN` instead of `DATEDIFF`.  | 
 | 83 | + | 
 | 84 | +```sql  | 
 | 85 | +SELECT   | 
 | 86 | + activity_date AS day,   | 
 | 87 | + COUNT(DISTINCT user_id) AS active_users  | 
 | 88 | +FROM   | 
 | 89 | + Activity  | 
 | 90 | +WHERE   | 
 | 91 | + activity_date BETWEEN DATE_SUB('2019-07-27', INTERVAL 29 DAY) AND '2019-07-27'  | 
 | 92 | +GROUP BY activity_date;  | 
 | 93 | +```  | 
 | 94 | + | 
 | 95 | +---  | 
 | 96 | + | 
 | 97 | +## 🐍 Pandas Solution (Python)  | 
 | 98 | +#### Explanation:  | 
 | 99 | +- Filter activity records for the **last 30 days**.  | 
 | 100 | +- **Group by `activity_date`** and count **unique `user_id`s**.  | 
 | 101 | +- **Ignore days with zero active users**.  | 
 | 102 | + | 
 | 103 | +```python  | 
 | 104 | +import pandas as pd  | 
 | 105 | + | 
 | 106 | +def daily_active_users(activity: pd.DataFrame) -> pd.DataFrame:  | 
 | 107 | + # Filter data within the last 30 days (ending on '2019-07-27')  | 
 | 108 | + filtered = activity[(activity["activity_date"] >= "2019-06-28") & (activity["activity_date"] <= "2019-07-27")]  | 
 | 109 | + | 
 | 110 | + # Group by day and count unique users  | 
 | 111 | + result = filtered.groupby("activity_date")["user_id"].nunique().reset_index()  | 
 | 112 | + | 
 | 113 | + # Rename columns  | 
 | 114 | + result.columns = ["day", "active_users"]  | 
 | 115 | + return result  | 
 | 116 | +```  | 
 | 117 | + | 
 | 118 | +---  | 
 | 119 | + | 
 | 120 | +## 📁 File Structure  | 
 | 121 | +```  | 
 | 122 | +📂 User-Activity-Past-30-Days  | 
 | 123 | +│── 📜 README.md  | 
 | 124 | +│── 📜 solution.sql  | 
 | 125 | +│── 📜 solution_between.sql  | 
 | 126 | +│── 📜 solution_pandas.py  | 
 | 127 | +│── 📜 test_cases.sql  | 
 | 128 | +```  | 
 | 129 | + | 
 | 130 | +---  | 
 | 131 | + | 
 | 132 | +## 🔗 Useful Links  | 
 | 133 | +- 📖 [LeetCode Problem](https://leetcode.com/problems/user-activity-for-the-past-30-days-i/)  | 
 | 134 | +- 📚 [SQL Date Functions](https://www.w3schools.com/sql/sql_dates.asp)  | 
 | 135 | +- 🐍 [Pandas Documentation](https://pandas.pydata.org/docs/)  | 
 | 136 | +```  | 
 | 137 | +
  | 
 | 138 | +This README includes:  | 
 | 139 | +- **Clear problem statement**  | 
 | 140 | +- **Input and output tables**  | 
 | 141 | +- **Multiple SQL solutions with explanations**  | 
 | 142 | +- **Pandas solution in Python**  | 
 | 143 | +- **File structure for a clean GitHub repo**  | 
 | 144 | +- **Useful links**  | 
 | 145 | +
  | 
 | 146 | +Let me know if you need any changes! 🚀  | 
0 commit comments