문제를 이해하고 있다면 바로 풀이를 보면 됨
전체 코드로 바로 넘어가도 됨
마음대로 번역해서 오역이 있을 수 있음
Table: Activity
| Column Name | Type |
|---|---|
| user_id | int |
| session_id | int |
| activity_date | date |
| activity_type | enum |
이 테이블은 중복된 행이 있을 수 있다.
activity_type 컬럼은 타입('open_session', 'end_session', 'scroll_down', 'send_message')의 ENUM(category)이다.
테이블은 소셜 미디어 웹사이트에 대한 유저 활동을 보여준다.
2019년 7월 27일을 포함하여 30일 동안의 일일 활동 사용자 수를 구하는 솔루션을 작성해라. 사용ㅈ가 특정일에 최소 한 번 이상 활동을 했다면 해당일은 활동일이다.
Input:
Activity table:
| user_id | session_id | activity_date | activity_type |
|---|---|---|---|
| 1 | 1 | 2019-07-20 | open_session |
| 1 | 1 | 2019-07-20 | scroll_down |
| 1 | 1 | 2019-07-20 | end_session |
| 2 | 4 | 2019-07-20 | open_session |
| 2 | 4 | 2019-07-21 | send_message |
| 2 | 4 | 2019-07-21 | end_session |
| 3 | 2 | 2019-07-21 | open_session |
| 3 | 2 | 2019-07-21 | send_message |
| 3 | 2 | 2019-07-21 | end_session |
| 4 | 3 | 2019-06-25 | open_session |
| 4 | 3 | 2019-06-25 | end_session |
Output:
| day | active_users |
|---|---|
| 2019-07-20 | 2 |
| 2019-07-21 | 2 |
-- Write your PostgreSQL query statement below
select activity_date as day, count(distinct user_id) as active_users
from Activity
where activity_date > '2019-06-27' and activity_date <= '2019-07-27'
group by activity_date