https://leetcode.com/problems/user-activity-for-the-past-30-days-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
Write an SQL query 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.
정답
# 1
SELECT activity_date day
, COUNT(DISTINCT user_id) active_users
FROM Activity
WHERE activity_date BETWEEN DATE_ADD('2019-07-27', INTERVAL -29 DAY)
AND '2019-07-27'
GROUP BY activity_date;
# 2
SELECT activity_date AS day
, COUNT(DISTINCT user_id) AS active_users
FROM Activity
WHERE DATEDIFF('2019-07-27', activity_date)<30 AND activity_date <= '2019-07-27'
GROUP BY activity_date;
'🛠️Skill > CodingTest' 카테고리의 다른 글
[프로그래머스] 식품분류별 가장 비싼 식품의 정보 조회하기 / GROUP BY (0) | 2022.11.20 |
---|---|
[프로그래머스] 즐겨찾기가 가장 많은 식당 정보 출력하기 / GROUP BY (0) | 2022.11.18 |
[프로그래머스] Lv3. 즐겨찾기가 가장 많은 식당 정보 출력하기 / GROUPBY (0) | 2022.11.18 |
[프로그래머스] LV2. 재구매가 일어난 상품과 회원 리스트 구하기 / 가격대 별 상품 개수 구하기 / 입양시각구하기(1) GROUP BY (0) | 2022.11.09 |
[Leetcode] 185. Department Top Three Salaries / 그룹별 TOP3 순위 구하기/DENSE_RANK (0) | 2022.11.07 |
댓글