Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d390a8c

Browse files
Update readme.md
1 parent 8b78131 commit d390a8c

File tree

1 file changed

+189
-0
lines changed
  • LeetCode SQL 50 Solution/1070. Product Sales Analysis III

1 file changed

+189
-0
lines changed
Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
Here's the **README.md** file including **MySQL, Window SQL, and Pandas solutions** for **LeetCode 1070 - Product Sales Analysis III**.
2+
3+
---
4+
5+
# **1070. Product Sales Analysis III**
6+
7+
## **Problem Statement**
8+
You are given two tables:
9+
10+
- `Sales` (contains sales data including `product_id`, `year`, `quantity`, and `price`).
11+
- `Product` (contains `product_id` and `product_name`).
12+
13+
Each `product_id` in `Sales` is a **foreign key** referring to the `Product` table.
14+
15+
### **Sales Table**
16+
```
17+
+---------+------------+------+----------+-------+
18+
| sale_id | product_id | year | quantity | price |
19+
+---------+------------+------+----------+-------+
20+
| int | int | int | int | int |
21+
+---------+------------+------+----------+-------+
22+
```
23+
- `(sale_id, year)` is the **primary key** (unique values).
24+
- `product_id` refers to the `Product` table.
25+
- `price` represents the **per unit price** of the product in that year.
26+
27+
### **Product Table**
28+
```
29+
+------------+--------------+
30+
| product_id | product_name |
31+
+------------+--------------+
32+
| int | varchar |
33+
+------------+--------------+
34+
```
35+
- `product_id` is the **primary key** of this table.
36+
37+
### **Task:**
38+
Find the `product_id`, `first_year`, `quantity`, and `price` for **the first year a product was sold**.
39+
40+
---
41+
42+
## **Example 1:**
43+
44+
### **Input:**
45+
**Sales Table**
46+
```
47+
+---------+------------+------+----------+-------+
48+
| sale_id | product_id | year | quantity | price |
49+
+---------+------------+------+----------+-------+
50+
| 1 | 100 | 2008 | 10 | 5000 |
51+
| 2 | 100 | 2009 | 12 | 5000 |
52+
| 7 | 200 | 2011 | 15 | 9000 |
53+
+---------+------------+------+----------+-------+
54+
```
55+
56+
**Product Table**
57+
```
58+
+------------+--------------+
59+
| product_id | product_name |
60+
+------------+--------------+
61+
| 100 | Nokia |
62+
| 200 | Apple |
63+
| 300 | Samsung |
64+
+------------+--------------+
65+
```
66+
67+
### **Output:**
68+
```
69+
+------------+------------+----------+-------+
70+
| product_id | first_year | quantity | price |
71+
+------------+------------+----------+-------+
72+
| 100 | 2008 | 10 | 5000 |
73+
| 200 | 2011 | 15 | 9000 |
74+
+------------+------------+----------+-------+
75+
```
76+
77+
### **Explanation:**
78+
- **Product 100 (Nokia):** First sold in **2008** with **10 units** at **5000** price.
79+
- **Product 200 (Apple):** First sold in **2011** with **15 units** at **9000** price.
80+
81+
---
82+
83+
## **SQL Solutions**
84+
85+
### **1️⃣ Standard MySQL Solution**
86+
```sql
87+
SELECT
88+
product_id,
89+
year AS first_year,
90+
quantity,
91+
price
92+
FROM
93+
Sales
94+
WHERE
95+
(product_id, year) IN (
96+
SELECT
97+
product_id,
98+
MIN(year) AS year
99+
FROM
100+
Sales
101+
GROUP BY
102+
product_id
103+
);
104+
```
105+
#### **Explanation:**
106+
1. **Subquery (`MIN(year)`)** → Finds the **first year** (`MIN(year)`) each `product_id` was sold.
107+
2. **Filter the main table** → Selects rows matching the **earliest year** for each product.
108+
109+
---
110+
111+
### **2️⃣ Window Function (SQL) Solution**
112+
```sql
113+
WITH RankedSales AS (
114+
SELECT
115+
product_id,
116+
year AS first_year,
117+
quantity,
118+
price,
119+
RANK() OVER (PARTITION BY product_id ORDER BY year ASC) AS rnk
120+
FROM Sales
121+
)
122+
SELECT product_id, first_year, quantity, price
123+
FROM RankedSales
124+
WHERE rnk = 1;
125+
```
126+
#### **Explanation:**
127+
1. **`RANK() OVER (PARTITION BY product_id ORDER BY year ASC)`**
128+
- Assigns **rank 1** to the first sale per `product_id`.
129+
2. **Filter (`WHERE rnk = 1`)**
130+
- Retrieves **only the first sale per product**.
131+
132+
---
133+
134+
## **Pandas Solution (Python)**
135+
```python
136+
import pandas as pd
137+
138+
# Sample Data
139+
sales_data = {'sale_id': [1, 2, 7],
140+
'product_id': [100, 100, 200],
141+
'year': [2008, 2009, 2011],
142+
'quantity': [10, 12, 15],
143+
'price': [5000, 5000, 9000]}
144+
145+
# Create DataFrame
146+
sales_df = pd.DataFrame(sales_data)
147+
148+
# Find the first sale per product
149+
first_sales = sales_df.loc[sales_df.groupby('product_id')['year'].idxmin(), ['product_id', 'year', 'quantity', 'price']]
150+
151+
# Rename columns
152+
first_sales.rename(columns={'year': 'first_year'}, inplace=True)
153+
154+
print(first_sales)
155+
```
156+
157+
### **Explanation:**
158+
1. **Create DataFrame** → Convert `Sales` table into Pandas DataFrame.
159+
2. **Group by `product_id` and get the `idxmin()` of `year`** → Finds the first sale per product.
160+
3. **Retrieve `product_id`, `year`, `quantity`, and `price`**.
161+
4. **Rename `year` to `first_year`**.
162+
163+
---
164+
165+
## **File Structure**
166+
```
167+
LeetCode1070/
168+
├── problem_statement.md # Contains the problem description and constraints.
169+
├── sql_solution.sql # Contains the SQL solutions (Standard + Window Functions).
170+
├── pandas_solution.py # Contains the Pandas solution.
171+
├── README.md # Overview of the problem and available solutions.
172+
```
173+
174+
---
175+
176+
## **Useful Links**
177+
- [LeetCode Problem 1070](https://leetcode.com/problems/product-sales-analysis-iii/)
178+
- [SQL JOIN Documentation](https://www.w3schools.com/sql/sql_join.asp)
179+
- [Pandas GroupBy Documentation](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.groupby.html)
180+
181+
---
182+
183+
This README now includes:
184+
**MySQL Query**
185+
**Window SQL Query**
186+
**Pandas Python Solution**
187+
**File Structure & Useful Links**
188+
189+
🚀 **Now it's a complete guide!** 🚀

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /