Skip to content

Commit 1e64627

Browse files
committed
task: #1070
1 parent b8386c3 commit 1e64627

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ Have a good contributing!
4646
2. [Medium](./leetcode/medium/)
4747
- [176. Second Highest Salary](./leetcode/medium/176.%20Second%20Highest%20Salary.sql)
4848
- [184. Department Highest Salary](./leetcode/medium/184.%20Department%20Highest%20Salary.sql)
49+
- [1070. Product Sales Analysis III](./leetcode/medium/1070.%20Product%20Sales%20Analysis%203.sql)
4950
- [1158. Market Analysis 1](./leetcode/medium/1158.%20Market%20Analysis%201.sql)
5051

5152
## License
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
Question 1070. Product Sales Analysis 3
3+
Link: https://leetcode.com/problems/product-sales-analysis-iii/description/
4+
5+
Table: Sales
6+
7+
+-------------+-------+
8+
| Column Name | Type |
9+
+-------------+-------+
10+
| sale_id | int |
11+
| product_id | int |
12+
| year | int |
13+
| quantity | int |
14+
| price | int |
15+
+-------------+-------+
16+
(sale_id, year) is the primary key (combination of columns with unique values) of this table.
17+
product_id is a foreign key (reference column) to Product table.
18+
Each row records a sale of a product in a given year.
19+
A product may have multiple sales entries in the same year.
20+
Note that the per-unit price.
21+
22+
Write a solution to find all sales that occurred in the first year each product was sold.
23+
24+
For each product_id, identify the earliest year it appears in the Sales table.
25+
26+
Return all sales entries for that product in that year.
27+
28+
Return a table with the following columns: product_id, first_year, quantity, and price.
29+
Return the result in any order.
30+
*/
31+
32+
SELECT
33+
s1.product_id,
34+
s1.year AS first_year,
35+
s1.quantity,
36+
s1.price
37+
FROM Sales AS s1
38+
WHERE s1.year = (
39+
SELECT MIN(s2.year)
40+
FROM Sales AS s2
41+
WHERE s1.product_id = s2.product_id
42+
)

0 commit comments

Comments
 (0)