-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathactiveUsers1454.sql
More file actions
100 lines (79 loc) · 2.74 KB
/
Copy pathactiveUsers1454.sql
File metadata and controls
100 lines (79 loc) · 2.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
Table: Accounts
+---------------+---------+
| Column Name | Type |
+---------------+---------+
| id | int |
| name | varchar |
+---------------+---------+
id is the primary key for this table.
This table contains the account id and the user name of each account.
Table: Logins
+---------------+---------+
| Column Name | Type |
+---------------+---------+
| id | int |
| login_date | date |
+---------------+---------+
There is no primary key for this table, it may contain duplicates.
This table contains the account id of the user who logged in and the login date. A user may log in multiple times in the day.
Active users are those who logged in to their accounts for five or more consecutive days.
Write an SQL query to find the id and the name of active users.
Return the result table ordered by id.
The query result format is in the following example.
Example 1:
Input:
Accounts table:
+----+----------+
| id | name |
+----+----------+
| 1 | Winston |
| 7 | Jonathan |
+----+----------+
Logins table:
+----+------------+
| id | login_date |
+----+------------+
| 7 | 2020-05-30 |
| 1 | 2020-05-30 |
| 7 | 2020-05-31 |
| 7 | 2020-06-01 |
| 7 | 2020-06-02 |
| 7 | 2020-06-02 |
| 7 | 2020-06-03 |
| 1 | 2020-06-07 |
| 7 | 2020-06-10 |
+----+------------+
Output:
+----+----------+
| id | name |
+----+----------+
| 7 | Jonathan |
+----+----------+
Explanation:
User Winston with id = 1 logged in 2 times only in 2 different days, so, Winston is not an active user.
User Jonathan with id = 7 logged in 7 times in 6 different days, five of them were consecutive days, so, Jonathan is an active user.
Follow up: Could you write a general solution if the active users are those who logged in to their accounts for n or more consecutive days?
# Write your MySQL query statement below
select Accounts.id, Accounts.name
from Accounts
where Accounts.id in (
select a.id
from Logins a, Logins b, Logins c, Logins d, Logins e
where a.id = b.id and b.id = c.id and c.id = d.id and d.id = e.id and
DATE_SUB(a.login_date, INTERVAL 1 DAY) = b.login_date and
DATE_SUB(b.login_date, INTERVAL 1 DAY) = c.login_date and
DATE_SUB(c.login_date, INTERVAL 1 DAY) = d.login_date and
DATE_SUB(d.login_date, INTERVAL 1 DAY) = e.login_date
group by a.id, a.login_date
)
order by Accounts.id
#925 ms solution
# Write your MySQL query statement below
select distinct d.id, a.name from
(select id, login_date, date_add(login_date, interval -row_number()
over(partition by id order by login_date) day) as grouped_date from Logins GROUP BY 1,2) d
left join Accounts a
on d.id = a.id
group by id, grouped_date
having count(grouped_date) >= 5
order by d.id;