|
1 |
| ---1369. Get the Second Most Recent Activity |
2 |
| --- |
3 |
| ---Table: UserActivity |
4 |
| --- |
5 |
| ---+---------------+---------+ |
6 |
| ---| Column Name | Type | |
7 |
| ---+---------------+---------+ |
8 |
| ---| username | varchar | |
9 |
| ---| activity | varchar | |
10 |
| ---| startDate | Date | |
11 |
| ---| endDate | Date | |
12 |
| ---+---------------+---------+ |
13 |
| ---This table does not contain primary key. |
14 |
| ---This table contain information about the activity performed of each user in a period of time. |
15 |
| ---A person with username performed a activity from startDate to endDate. |
16 |
| --- |
17 |
| ---Write an SQL query to show the second most recent activity of each user. |
18 |
| --- |
19 |
| ---If the user only has one activity, return that one. |
20 |
| --- |
21 |
| ---A user can't perform more than one activity at the same time. Return the result table in any order. |
22 |
| --- |
23 |
| ---The query result format is in the following example: |
24 |
| --- |
25 |
| ---UserActivity table: |
26 |
| ---+------------+--------------+-------------+-------------+ |
27 |
| ---| username | activity | startDate | endDate | |
28 |
| ---+------------+--------------+-------------+-------------+ |
29 |
| ---| Alice | Travel | 2020-02-12 | 2020-02-20 | |
30 |
| ---| Alice | Dancing | 2020-02-21 | 2020-02-23 | |
31 |
| ---| Alice | Travel | 2020-02-24 | 2020-02-28 | |
32 |
| ---| Bob | Travel | 2020-02-11 | 2020-02-18 | |
33 |
| ---+------------+--------------+-------------+-------------+ |
34 |
| --- |
35 |
| ---Result table: |
36 |
| ---+------------+--------------+-------------+-------------+ |
37 |
| ---| username | activity | startDate | endDate | |
38 |
| ---+------------+--------------+-------------+-------------+ |
39 |
| ---| Alice | Dancing | 2020-02-21 | 2020-02-23 | |
40 |
| ---| Bob | Travel | 2020-02-11 | 2020-02-18 | |
41 |
| ---+------------+--------------+-------------+-------------+ |
42 |
| --- |
43 |
| ---The most recent activity of Alice is Travel from 2020-02-24 to 2020-02-28, before that she was dancing from 2020-02-21 to 2020-02-23. |
44 |
| ---Bob only has one record, we just take that one. |
45 |
| - |
46 | 1 | --# Write your MySQL query statement below
|
47 | 2 | --credit: https://leetcode.com/problems/get-the-second-most-recent-activity/discuss/530992/MySQL-solution-184ms-no-subquery
|
48 | 3 |
|
|
0 commit comments