Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ Have a good contributing!
2. [Medium](./leetcode/medium/)
- [176. Second Highest Salary](./leetcode/medium/176.%20Second%20Highest%20Salary.sql)
- [184. Department Highest Salary](./leetcode/medium/184.%20Department%20Highest%20Salary.sql)
- [1070. Product Sales Analysis III](./leetcode/medium/1070.%20Product%20Sales%20Analysis%203.sql)
- [1158. Market Analysis 1](./leetcode/medium/1158.%20Market%20Analysis%201.sql)

## License
Expand Down
42 changes: 42 additions & 0 deletions leetcode/medium/1070. Product Sales Analysis 3.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
Question 1070. Product Sales Analysis 3
Link: https://leetcode.com/problems/product-sales-analysis-iii/description/

Table: Sales

+-------------+-------+
| Column Name | Type |
+-------------+-------+
| sale_id | int |
| product_id | int |
| year | int |
| quantity | int |
| price | int |
+-------------+-------+
(sale_id, year) is the primary key (combination of columns with unique values) of this table.
product_id is a foreign key (reference column) to Product table.
Each row records a sale of a product in a given year.
A product may have multiple sales entries in the same year.
Note that the per-unit price.

Write a solution to find all sales that occurred in the first year each product was sold.

For each product_id, identify the earliest year it appears in the Sales table.

Return all sales entries for that product in that year.

Return a table with the following columns: product_id, first_year, quantity, and price.
Return the result in any order.
*/

SELECT
s1.product_id,
s1.year AS first_year,
s1.quantity,
s1.price
FROM Sales AS s1
WHERE s1.year = (
SELECT MIN(s2.year)
FROM Sales AS s2
WHERE s1.product_id = s2.product_id
)