forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 14
/
active-users.sql
34 lines (33 loc) · 899 Bytes
/
active-users.sql
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
# Time: O(nlogn)
# Space: O(n)
SELECT DISTINCT
r.id,
r.name
FROM
(SELECT a_l.id,
a_l.name,
@accu := CASE
WHEN a_l.name = @prev AND
DATEDIFF(a_l.login_date, @login_date) = 1
THEN @accu + 1
ELSE 1
END AS accu,
@prev := a_l.name AS prev,
@login_date := a_l.login_date AS login_date
FROM (
(SELECT DISTINCT
a.id,
a.name,
l.login_date
FROM accounts a
LEFT JOIN logins l
ON a.id = l.id
ORDER BY a.id,
a.name,
l.login_date) a_l,
(SELECT @accu := 0,
@prev := "",
@login_date := "") init
)
) r
WHERE r.accu = 5;