๐ข ๋ณธ ํฌ์คํ ์ ํ์ฉ๋๋ ๊ธฐ๋ณธ ๋ฌธ์ ๋ฐ ์๋ฃ ์ถ์ฒ๋
๋ฆฌํธ์ฝ๋ Problems, https://leetcode.com/problemset/all/์์ ๋ฐํ๋๋ค.
โ ๋ฌธ์
https://leetcode.com/problems/market-analysis-i/description/
Market Analysis I - LeetCode
Can you solve this real interview question? Market Analysis I - Table: Users +----------------+---------+ | Column Name | Type | +----------------+---------+ | user_id | int | | join_date | date | | favorite_brand | varchar | +----------------+---------+ u
leetcode.com
Table : Users
Table : Orders
Table : Items
Write an SQL query to find for each user, the join date and the number of orders they made as a buyer in 2019.
Return the result table in any order.
The query result format is in the following example.
โ ํ์ด
๋ฌธ์ ์๊ตฌ์ฌํญ
์ฌ์ฉ์์ ์์ด๋, ๊ฐ์ ๋ ์ง์ 2019๋ ๋ ๊ตฌ๋งค ์๋ฅผ ์ถ๋ ฅํด์ผ ํ๋ค.
* ์ฃผ์ํ ์ ์ ๊ตฌ๋งค ์๊ฐ ์๋ค๋ฉด 0์ผ๋ก ์ถ๋ ฅํด์ผ ํ๋ค๋ ๊ฒ.
2019 ์ฃผ๋ฌธ(๊ตฌ๋งค)์ด๋ ฅ์ count ํด์ผ ํ๋ฏ๋ก Orders ํ ์ด๋ธ์ ๊ธฐ์ค์ผ๋ก buyer์ user ์ ๋ณด๋ฅผ ์กฐ์ธํด์ฃผ๊ณ
๋ ์ง ํจ์๋ฅผ ํ์ฉํด์ order_date ์ฐ๋ = 2019 ๋ฅผ ์กฐ์ธ ์กฐ๊ฑด์ ๋ํด์คฌ๋ค.
๊ตฌ๋งค ์ด๋ ฅ์ด ์๋ ๊ฒฝ์ฐ์๋ ๋ฐ์ดํฐ ํ์ ์ถ๋ ฅํ๊ธฐ ์ํด ์ง๊ณ ๊ฒฐ๊ณผ์ ๋ํด IFNULL ํจ์๋ฅผ ์ฌ์ฉํ๋ค.
SELECT user_id AS buyer_id
,join_date
,IFNULL(COUNT(order_date), 0) AS orders_in_2019
FROM Users U
LEFT JOIN Orders O
ON U.user_id = O.buyer_id
AND YEAR(order_date) = '2019'
GROUP BY user_id
'Growth ๐ณ > Practice ๐ป' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Leet code] 1164. Product Price at a Given Date (0) | 2023.07.17 |
---|---|
[LeetCode] 1321. Restaurant Growth (3) | 2023.06.17 |
[LeetCode] 1070. Product Sales Analysis III (0) | 2023.05.22 |
[LeetCode] 1045. Customers Who Bought All Products (0) | 2023.05.18 |
[LeetCode] 608. Tree Node (0) | 2023.05.16 |