๐ข ๋ณธ ํฌ์คํ ์ ํ์ฉ๋๋ ๊ธฐ๋ณธ ๋ฌธ์ ๋ฐ ์๋ฃ ์ถ์ฒ๋
๋ฆฌํธ์ฝ๋ Problems, https://leetcode.com/problemset/all/์์ ๋ฐํ๋๋ค.
โ ๋ฌธ์
https://leetcode.com/problems/product-sales-analysis-iii/description/
Product Sales Analysis III - LeetCode
Can you solve this real interview question? Product Sales Analysis III - Table: Sales +-------------+-------+ | Column Name | Type | +-------------+-------+ | sale_id | int | | product_id | int | | year | int | | quantity | int | | price | int | +---------
leetcode.com
Table : Sales
Table : Product
Write an SQL query that selects the product id, year, quantity, and price for the first year of every product sold.
Return the resulting table in any order.
The query result format is in the following example.
โ ํ์ด
๋ฌธ์ ์๊ตฌ์ฌํญ
์ํ id, ์ฐ๋, ์๋, ์ฐ๋(ํด๋น ์ ํ์ด ๋ชจ๋ ํ๋งค๋ ์ฒซ ํด) ๋ฅผ ์ถ๋ ฅํด์ผ ํ๋ค.
์ํ๋ณ๋ก ํ๋งค๋ ์ฐ๋์ค ์ฒซ ํด๋ฅผ ์ถ๋ ฅํ๊ธฐ ์ํด product_id๋ณ ๊ทธ๋ฃนํํ์ฌ ์ต์๊ฐ(min(year))์ ์ถ๋ ฅํ ๊ฒ๊ณผ ๋์ผํ ํ์ ์ปฌ๋ผ์ ์ถ๋ ฅํ๋๋ก where์ ์ ์กฐ๊ฑด์ผ๋ก ์ค์ ํ๋ค.
SELECT product_id AS product_id,
year AS first_year,
quantity AS quantity,
price AS price
FROM Sales
WHERE (product_id, year) IN (SELECT product_id, min(year)
FROM Sales
GROUP BY 1)
'Growth ๐ณ > Practice ๐ป' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[LeetCode] 1321. Restaurant Growth (3) | 2023.06.17 |
---|---|
[LeetCode] 1158. Market Analysis I (0) | 2023.06.08 |
[LeetCode] 1045. Customers Who Bought All Products (0) | 2023.05.18 |
[LeetCode] 608. Tree Node (0) | 2023.05.16 |
[LeetCode] 602. Friend Requests II: Who Has the Most Friends (0) | 2023.05.12 |