Growth ๐ŸŒณ/Practice ๐Ÿ’ป

[LeetCode] 1070. Product Sales Analysis III

์ธ” 2023. 5. 22. 00:38

๐Ÿ“ข ๋ณธ ํฌ์ŠคํŒ…์— ํ™œ์šฉ๋˜๋Š” ๊ธฐ๋ณธ ๋ฌธ์ œ ๋ฐ ์ž๋ฃŒ ์ถœ์ฒ˜๋Š”

       ๋ฆฌํŠธ์ฝ”๋“œ 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 idyearquantity, 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