Growth ๐ŸŒณ/Practice ๐Ÿ’ป

[Leet code] 1084. Sales Analysis III

์ธ” 2023. 10. 10. 23:56

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

       ๋ฆฌํŠธ์ฝ”๋“œ Problems, https://leetcode.com/problemset/all/์ž„์„ ๋ฐํž™๋‹ˆ๋‹ค.


โœ” ๋ฌธ์ œ

https://leetcode.com/problems/sales-analysis-iii/

 

LeetCode - The World's Leading Online Programming Learning Platform

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

 

Table : Product

Table : Sales

Write a solution to report the products that were only sold in the first quarter of 2019. That is, between 2019-01-01 and 2019-03-31 inclusive.

Return the result table in any order.


โœ” ํ’€์ด

  ๋ฌธ์ œ ์š”๊ตฌ์‚ฌํ•ญ  

์ƒํ’ˆ์ •๋ณด(์ƒํ’ˆid, ์ƒํ’ˆ๋ช…, ์ƒํ’ˆ๋‹จ๊ฐ€) ํ…Œ์ด๋ธ”๊ณผ ํŒ๋งค์ •๋ณด ํ…Œ์ด๋ธ”์„ ํ™œ์šฉํ•˜์—ฌ 2019๋…„ 1๋ถ„๊ธฐ(2019.01.01 ~ 2019.03.31)์—๋งŒ ํŒ๋งค๋˜์—ˆ๋˜ ์ œํ’ˆ์˜ id์™€ ์ด๋ฆ„์„ ์ถœ๋ ฅํ•ด์•ผ ํ•œ๋‹ค.

โ‘  1๋ถ„๊ธฐ ํŒ๋งค ์ด๋ ฅ์„ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด Sales ํ…Œ์ด๋ธ”์„ ๊ธฐ์ค€์œผ๋กœ LEFTJOINํ•ด์„œ sale_date๋ฅผ 1๋ถ„์œ„๋กœ ์ œํ•œํ•˜๊ณ  <์กฐ๊ฑด1>

โ‘ก [ โ‘ ] ์„ ์ถฉ์กฑํ•˜๋ฉด์„œ 1๋ถ„๊ธฐ ๊ธฐ๊ฐ„ ์™ธ์— ํŒ๋งค๋˜์ง€ ์•Š์€ product_id <์กฐ๊ฑด2> ๋ฅผ ์ฐพ๊ธฐ ์œ„ํ•ด NOT IN ์„ ์‚ฌ์šฉํ–ˆ๋‹ค. 

SELECT DISTINCT T1.product_id, T2.product_name
 FROM Sales T1
 LEFT JOIN Product T2
 ON T1.product_id = T2.product_id
 WHERE sale_date BETWEEN '2019-01-01' AND '2019-03-31'
 AND T1.product_id NOT IN (SELECT product_id
                            FROM Sales
                            WHERE sale_date > '2019-03-31'
                            OR sale_date < '2019-01-01')