Skip to content

Commit d05784b

Browse files
committed
Update readme.md
1 parent 79eaf89 commit d05784b

File tree

1 file changed

+125
-0
lines changed
  • LeetCode SQL 50 Solution/1484. Group Sold Products By The Date

1 file changed

+125
-0
lines changed
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
Here’s a well-structured `README.md` for **LeetCode 1484 - Group Sold Products By The Date**, formatted for a GitHub repository:
2+
3+
```md
4+
# 🛍️ Group Sold Products By The Date - LeetCode 1484
5+
6+
## 📌 Problem Statement
7+
You are given a table **Activities** that contains records of products sold on different dates.
8+
9+
Your task is to return:
10+
- The **number of distinct products** sold on each date.
11+
- A **comma-separated string** of the product names, sorted **lexicographically**.
12+
13+
The result should be **ordered by `sell_date`**.
14+
15+
---
16+
17+
## 📊 Table Structure
18+
19+
### **Activities Table**
20+
| Column Name | Type |
21+
| ----------- | ------- |
22+
| sell_date | date |
23+
| product | varchar |
24+
25+
- This table **does not** have a **primary key**.
26+
- It may **contain duplicate entries**.
27+
28+
---
29+
30+
## 📊 Example 1:
31+
### **Input:**
32+
#### **Activities Table**
33+
| sell_date | product |
34+
| ---------- | ---------- |
35+
| 2020-05-30 | Headphone |
36+
| 2020-06-01 | Pencil |
37+
| 2020-06-02 | Mask |
38+
| 2020-05-30 | Basketball |
39+
| 2020-06-01 | Bible |
40+
| 2020-06-02 | Mask |
41+
| 2020-05-30 | T-Shirt |
42+
43+
### **Output:**
44+
| sell_date | num_sold | products |
45+
| ---------- | -------- | ---------------------------- |
46+
| 2020-05-30 | 3 | Basketball,Headphone,T-Shirt |
47+
| 2020-06-01 | 2 | Bible,Pencil |
48+
| 2020-06-02 | 1 | Mask |
49+
50+
### **Explanation:**
51+
- `2020-05-30`: Sold items → _(Headphone, Basketball, T-Shirt)_
52+
- Sorted → **"Basketball, Headphone, T-Shirt"**
53+
- `2020-06-01`: Sold items → _(Pencil, Bible)_
54+
- Sorted → **"Bible, Pencil"**
55+
- `2020-06-02`: Sold item → _(Mask)_
56+
- **"Mask"** (only one item)
57+
58+
---
59+
60+
## 🖥 SQL Solution
61+
62+
### ✅ **Using `GROUP_CONCAT` with `DISTINCT`**
63+
#### **Explanation:**
64+
- Use `COUNT(DISTINCT product)` to get the **number of distinct products**.
65+
- Use `GROUP_CONCAT(DISTINCT product ORDER BY product ASC)` to **join product names in alphabetical order**.
66+
- Group by `sell_date`, then order the result by `sell_date`.
67+
68+
```sql
69+
SELECT
70+
sell_date,
71+
COUNT(DISTINCT product) AS num_sold,
72+
GROUP_CONCAT(DISTINCT product ORDER BY product ASC SEPARATOR ',') AS products
73+
FROM Activities
74+
GROUP BY sell_date
75+
ORDER BY sell_date ASC;
76+
```
77+
78+
---
79+
80+
## 🐍 Pandas Solution (Python)
81+
#### **Explanation:**
82+
- **Group by `sell_date`**.
83+
- Use `.nunique()` to count distinct products.
84+
- Use `', '.join(sorted(set(products)))` to sort and concatenate product names.
85+
86+
```python
87+
import pandas as pd
88+
89+
def group_sold_products(activities: pd.DataFrame) -> pd.DataFrame:
90+
grouped_df = (
91+
activities.groupby("sell_date")["product"]
92+
.agg(lambda x: ", ".join(sorted(set(x))))
93+
.reset_index()
94+
)
95+
grouped_df["num_sold"] = grouped_df["product"].apply(lambda x: len(x.split(",")))
96+
return grouped_df.rename(columns={"product": "products"})
97+
```
98+
99+
---
100+
101+
## 📁 File Structure
102+
```
103+
📂 Group-Sold-Products
104+
│── 📜 README.md
105+
│── 📜 solution.sql
106+
│── 📜 solution_pandas.py
107+
│── 📜 test_cases.sql
108+
```
109+
110+
---
111+
112+
## 🔗 Useful Links
113+
- 📖 [LeetCode Problem](https://leetcode.com/problems/group-sold-products-by-the-date/)
114+
- 📚 [SQL `GROUP BY`](https://www.w3schools.com/sql/sql_groupby.asp)
115+
- 🐍 [Pandas `groupby()` Documentation](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.groupby.html)
116+
```
117+
118+
### Features of this `README.md`:
119+
✅ **Clear problem statement with tables**
120+
✅ **Example with step-by-step explanation**
121+
✅ **SQL and Pandas solutions with detailed breakdowns**
122+
✅ **File structure for easy organization**
123+
✅ **Helpful references for further learning**
124+
125+
Would you like any modifications? 🚀

0 commit comments

Comments
 (0)