๐ข ๋ณธ ํฌ์คํ ์ ํ์ฉ๋๋ ๊ธฐ๋ณธ ๋ฌธ์ ๋ฐ ์๋ฃ ์ถ์ฒ๋
๋ฆฌํธ์ฝ๋ Problems, https://leetcode.com/problemset/all/์์ ๋ฐํ๋๋ค.
โ๋ฌธ์
https://leetcode.com/problems/user-activity-for-the-past-30-days-i/
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 : Activity
Write a solution to find the daily active user count for a period of 30 days ending 2019-07-27 inclusively. A user was active on someday if they made at least one activity on that day.
Return the result table in any order.
The result format is in the following example.
โ ํ์ด
๋ฌธ์ ์๊ตฌ์ฌํญ
< ํน์ด์ฌํญ >
- ์ค๋ณต๊ฐ ์์.
- (์ปฌ๋ผ) activity๋ ๋ฐ์ดํฐ ํ์ ์ด ENUM์ผ๋ก ์ฌ๋ฌ๊ฐ ์ค์ ํ์ฉ๋ ๊ฐ๋ง ์ ์ฅ๋๊ฒ๋ ํ๋ค.
2019-07-27 ์ ํฌํจํ์ฌ 30์ผ ์ด๋ด์ ์ผ๋ณ ์ต์ ํ๊ฐ์ง ํ๋์ ํ ์ ์ ์๋ฅผ ์ถ๋ ฅํด์ผ ํ๋ค.
session์ ์ ์ ๋น ๋ถ์ฌ๋๋ ๊ฐ์ด๋ฏ๋ก ๋ณ๋๋ก ๊ทธ๋ฃนํ ํด์ค ํ์๊ฐ ์๋ค.
· ์ ์ ์๋ฅผ ํ์ ํ๊ณ ์ ํ๋ '๊ธฐ๊ฐ'
- 30์ผ ์ด๋ด์ด๋ฏ๋ก BETWEEN ~ AND ๋ก DATE_ADD('๊ธฐ์ค์ผ์', INTERVAL N DAY) ๋ฅผ ์ฌ์ฉํ๋ค.
๊ธฐํ ๋ค๋ฅธ ๋ ์ง ํจ์๋ ์ฌ์ฉ ๊ฐ๋ฅํ๋ค.
์ผ๋ณ๋ก ์นด์ดํธํด์ผํ๋ฏ๋ก activity_date๋ก ๊ทธ๋ฃนํํด์ฃผ๊ณ
ํน์ ์ผ์ ๋ด์์๋ ํ ์ ์ ๊ฐ ์ฌ๋ฌ๊ฐ์ง ํ๋์ ํ ์ ์์ผ๋ฏ๋ก id๋ DISTINCT(์ค๋ณต์ ๊ฑฐ)๋ฅผ ์ ์ฉํด์ฃผ์๋ค.
SELECT activity_date AS day, COUNT(DISTINCT user_id) AS active_users
FROM Activity
WHERE activity_date BETWEEN DATE_ADD('2019-07-27', INTERVAL -29 DAY) AND '2019-07-27'
GROUP BY activity_date
'Growth ๐ณ > Practice ๐ป' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Leet code] 1193. Monthly Transactions I (0) | 2023.09.21 |
---|---|
[Leet code] 1174. Immediate Food Delivery II (0) | 2023.09.15 |
[Leet code] 610. Triangle Judgement (0) | 2023.09.13 |
[Leet code] 1050. Actors and Directors Who Cooperated At Least Three Times (2) | 2023.09.08 |
[Leet code] 619. Biggest Single Number (0) | 2023.09.02 |