Algoritm/leetcode

[leetcode] 1141. User Activity for the Past 30 Days I

twoDeveloper 2023. 4. 19. 23:13

# 문제풀이

select activity_date as day, count(distinct user_id) as active_users
from activity
where datediff('2019-07-27', activity_date) < 30 and datediff('2019-07-27', activity_date) >= 0
group by activity_date;

- activity 테이블에는 기본키가 없으며, 중복키가 허용됨

- distinct를 사용하여 user_id를 중복 제거

- datediff() 함수를 사용하여 날짜 차이 출력

 

* DATEDIFF()

- 두 날짜 간의 차이를 일 수 로 계산하는 MySQL 내장 함수

- 첫 번째 인수에는 뒤에 오는 날짜 값과의 차이를 구할 기준 날짜를, 두 번째 인수에는 기준 날짜와의 차이를 구할 날짜 값을 입력

ex)

DATEDIFF(date1, date2)

SELECT DATEDIFF('2023-04-30', '2023-04-01');
# 29가 반환

 

# 문제출처

https://leetcode.com/problems/user-activity-for-the-past-30-days-i/?envType=study-plan&id=sql-i 

 

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