-
leetcode - 1107. New Users Daily CountSQL 문제풀이 2022. 7. 15. 16:48
난이도 : Medium
Table: Traffic
+---------------+---------+ | Column Name | Type | +---------------+---------+ | user_id | int | | activity | enum | | activity_date | date | +---------------+---------+ There is no primary key for this table, it may have duplicate rows. The activity column is an ENUM type of ('login', 'logout', 'jobs', 'groups', 'homepage').
Write an SQL query to reports for every date within at most 90 days from today, the number of users that logged in for the first time on that date. Assume today is 2019-06-30.
Return the result table in any order.
The query result format is in the following example.
Example 1:
Input: Traffic table: +---------+----------+---------------+ | user_id | activity | activity_date | +---------+----------+---------------+ | 1 | login | 2019-05-01 | | 1 | homepage | 2019-05-01 | | 1 | logout | 2019-05-01 | | 2 | login | 2019-06-21 | | 2 | logout | 2019-06-21 | | 3 | login | 2019-01-01 | | 3 | jobs | 2019-01-01 | | 3 | logout | 2019-01-01 | | 4 | login | 2019-06-21 | | 4 | groups | 2019-06-21 | | 4 | logout | 2019-06-21 | | 5 | login | 2019-03-01 | | 5 | logout | 2019-03-01 | | 5 | login | 2019-06-21 | | 5 | logout | 2019-06-21 | +---------+----------+---------------+ Output: +------------+-------------+ | login_date | user_count | +------------+-------------+ | 2019-05-01 | 1 | | 2019-06-21 | 2 | +------------+-------------+ Explanation: Note that we only care about dates with non zero user count. The user with id 5 first logged in on 2019-03-01 so he's not counted on 2019-06-21.
WITH sub AS ( SELECT user_id , MIN(activity_date) login_date FROM Traffic WHERE activity = 'login' GROUP BY user_id ) SELECT login_date , COUNT(distinct user_id) user_count FROM sub WHERE login_date BETWEEN DATE_ADD('2019-06-30', INTERVAL -90 DAY) AND '2019-06-30' GROUP BY login_date
Accepted (32.78%)
'SQL 문제풀이' 카테고리의 다른 글
leetcode - 1565. Unique Orders and Customers Per Month (0) 2022.07.18 leetcode - 1112. Highest Grade For Each Student (0) 2022.07.15 leetcode - 1543. Fix Product Name Format (0) 2022.07.14 leetcode - 1527. Patients With a Condition (0) 2022.07.14 leetcode - 1517. Find Users With Valid E-Mails (0) 2022.07.14