[1141_EASY] User Activity for the Past 30 Days I

2022. 12. 8. 20:58·SQL/LeetCode

Table: Activity

+---------------+---------+
| Column Name   | Type    |
+---------------+---------+
| user_id       | int     |
| session_id    | int     |
| activity_date | date    |
| activity_type | enum    |
+---------------+---------+
There is no primary key for this table, it may have duplicate rows.
The activity_type column is an ENUM of type ('open_session', 'end_session', 'scroll_down', 'send_message').
The table shows the user activities for a social media website. 
Note that each session belongs to exactly one user.

 

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.

The query result format is in the following example.

 

Example 1:

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            |
+------------+--------------+ 
Explanation: 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 > '2019-06-27' -- 간단한 날짜 같은 경우는 직접 계산하는게 DB에 부담을 주지 않음
AND activity_date < '2019-07-28'
GROUP BY 1
저작자표시 (새창열림)

'SQL > LeetCode' 카테고리의 다른 글

[1076#_EASY] Project Employees II  (0) 2022.12.14
[1075#_EASY] Project Employees I  (0) 2022.12.14
[603#_EASY]Consecutive Available Seats  (0) 2022.10.29
[597_EASY] Friend Requests I : Overall Acceptance Rate  (0) 2022.10.04
[596_EASY] Classes More Than 5 Students  (0) 2022.10.04
'SQL/LeetCode' 카테고리의 다른 글
  • [1076#_EASY] Project Employees II
  • [1075#_EASY] Project Employees I
  • [603#_EASY]Consecutive Available Seats
  • [597_EASY] Friend Requests I : Overall Acceptance Rate
소금깨
소금깨
  • 소금깨
    고군분투 인생살이
    소금깨
  • 전체
    오늘
    어제
    • 분류 전체보기 (328)
      • SQL (271)
        • 프로그래머스 (27)
        • LeetCode (198)
        • Hacker Rank (27)
        • Solve SQL (1)
        • 개념 (15)
      • 데이터 분석 (16)
        • 참고하며 공부하기 (14)
      • 기타 (15)
        • 통계 (14)
      • 오류 (6)
      • 인생살이 (0)
        • 리뷰 (0)
        • 일기 (0)
      • 中文 (0)
      • TABLEAU (3)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    SQL
    파이썬을활용한시계열데이터분석AtoZ올인원패키지Online
    easy
    패스트캠퍼스후기
    medium
    프리미엄
    MySQL
    파이썬을활용한시계열데이터분석
    HACKER_RANK
    solvesql
    직장인인강
    패스트캠퍼스
    LeetCode
    패캠챌린지
    직장인자기계발
    group by
    해커랭크
    시계열데이터분석
    프로그래머스
    Hard
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
소금깨
[1141_EASY] User Activity for the Past 30 Days I
상단으로

티스토리툴바

단축키

내 블로그

내 블로그 - 관리자 홈 전환
Q
Q
새 글 쓰기
W
W

블로그 게시글

글 수정 (권한 있는 경우)
E
E
댓글 영역으로 이동
C
C

모든 영역

이 페이지의 URL 복사
S
S
맨 위로 이동
T
T
티스토리 홈 이동
H
H
단축키 안내
Shift + /
⇧ + /

* 단축키는 한글/영문 대소문자로 이용 가능하며, 티스토리 기본 도메인에서만 동작합니다.