๐ป ๋ฌธ์ ์ฃผ์ : User Activity for the Past 30 Days I - LeetCode
User Activity for the Past 30 Days I - LeetCode
Can you solve this real interview question? User Activity for the Past 30 Days I - Table: Activity +---------------+---------+ | Column Name | Type | +---------------+---------+ | user_id | int | | session_id | int | | activity_date | date | | activity_typ
leetcode.com
๐ป ๋ฌธ์
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. And please note that we do not care about days with zero active users.
๐ป ์ฝ๋ ๋ฏธ๋ฆฌ๋ณด๊ธฐ
select activity_date as day, count(distinct(user_id)) as active_users
from Activity
where activity_date between date_sub('2019-07-27', interval 29 day) and '2019-07-27'
group by activity_date
๐ป ๋ฌธ์ ํ์ด
์ด๋ฒ ๋ฌธ์ ๋ join์ด๋ ์๋ธ์ฟผ๋ฆฌ๋ฅผ ์ฌ์ฉํ ํ์๋ ์์ง๋ง ๋์ '๋ ์ง'๋ฅผ ์กฐ๊ฑด์ผ๋ก ํ๊ณ ์๊ธฐ ๋๋ฌธ์ ์ด ํจ์ ์ ์ ํผํด์ผ ํ๋ค.
๋ฌธ์ ๋ฅผ ํ์ดํ๊ธฐ์ ์์, <1์ 1์ผ ~ 1์ 5์ผ>๊น์ง์ ์ผ์๋ฅผ ๊ณ์ฐํด๋ณด์. 1์ผ, 2์ผ, 3์ผ, 4์ผ, ๊ทธ๋ฆฌ๊ณ 5์ผ. 5-1์ 4์์๋ ๋ถ๊ตฌํ๊ณ 1์ ์์์ผ๋ก ํฌํจํ๊ธฐ ๋๋ฌธ์ (ํน์ 5์ผ์ ์ข ๋ฃ์ผ๋ก ํฌํจํ๊ธฐ ๋๋ฌธ์) ์ผ์๋ ์ด 5๊ฐ ๋๋ค.
๊ทธ๋ ๋ค๋ฉด ์ด์ ๋ฌธ์ ๋ฅผ ๋ค์ ํ ๋ฒ ์ดํด๋ณด์. ๋ฌธ์ ๋ฅผ ๋ณด๋ฉด a period of 30 days ending 2019-07-27 ๋ฅผ ์กฐ๊ฑด์ผ๋ก ํ๊ณ ์๋ค. ์ฆ, ๋ค์ ๋งํด
1๏ธโฃ 2019-07-27๊น์ง 30์ผ ๋์ ์ผ์ผ ํ์ฑ ์ฌ์ฉ์ ์๋ฅผ ์ฐพ๋ ์ฟผ๋ฆฌ๋ฅผ ์์ฑํ๋ผ
๊ฐ ์ด๋ฒ ๋ฌธ์ ์ ํต์ฌ์ด ๋๋ค. ๊ทธ๋ฆฌ๊ณ ์ฐ๋ฆฌ๋ ์์์ผ์ ํฌํจํ๊ธฐ ๋๋ฌธ์ ๋จ์ ์ซ์๋ก๋ง ๊ณ์ฐํ๋ค๊ณ ์น๋ฉด (07์ 27์ผ - ??์ ??์ผ)์ 30์ด ์๋ 29๊ฐ ๋๋ค.
select
from Activity
where activity_date between date_sub('2019-07-27', interval 29 day) and '2019-07-27'
๋ฐ๋ผ์ ์ ์ฟผ๋ฆฌ์ฒ๋ผ ๊ธฐ์ค์ผ(2019-07-27)๋ก๋ถํฐ 29์ผ ๋บ ์์์ผ๋ก๋ถํฐ ๊ธฐ์ค์ผ(2019-07-27)๊น์ง ํ๋ ์ผ์๊ฐ ์๋ ๋ ์ฝ๋ ์ฐพ๋ ์ฟผ๋ฆฌ๋ฅผ where์ ๋ก ํ์ด๋ด๋ฉด ๋๋ค.
'SQL > LeetCode' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[SQL50] #26. Classes More Than 5 Students (1) | 2024.01.05 |
---|---|
[LeetCode][SQL50] #25. Product Sales Analysis III (1) | 2024.01.04 |
[LeetCode][SQL50] #23. Number of Unique Subjects Taught by Each Teacher (1) | 2024.01.03 |
[LeetCode][SQL50] #22. Game Play Analysis IV (0) | 2024.01.02 |
[LeetCode][SQL50] #21. Immediate Food Delivery II (1) | 2024.01.01 |