Growth ๐ŸŒณ/Practice ๐Ÿ’ป

[LeetCode] 1158. Market Analysis I

์ธ” 2023. 6. 8. 18:03

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

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