From 1e646271fd92eb75d8f20112a65bbc7efdb38768 Mon Sep 17 00:00:00 2001 From: ivanbyone Date: Mon, 30 Jun 2025 17:46:38 +0300 Subject: [PATCH] task: #1070 --- README.md | 1 + .../medium/1070. Product Sales Analysis 3.sql | 42 +++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 leetcode/medium/1070. Product Sales Analysis 3.sql diff --git a/README.md b/README.md index 216646d..8161efa 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/leetcode/medium/1070. Product Sales Analysis 3.sql b/leetcode/medium/1070. Product Sales Analysis 3.sql new file mode 100644 index 0000000..5e43548 --- /dev/null +++ b/leetcode/medium/1070. Product Sales Analysis 3.sql @@ -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 +)