[Leet code] 1084. Sales Analysis III
๐ข ๋ณธ ํฌ์คํ ์ ํ์ฉ๋๋ ๊ธฐ๋ณธ ๋ฌธ์ ๋ฐ ์๋ฃ ์ถ์ฒ๋
๋ฆฌํธ์ฝ๋ 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')