Skip to content

Commit 121459a

Browse files
add 1369
1 parent 6fab830 commit 121459a

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -917,6 +917,7 @@ _If you like this project, please leave me a star._ ★
917917

918918
| # | Title | Solutions | Video | Difficulty | Tag
919919
|-----|----------------|---------------|---------------|---------------|-------------
920+
|1369|[Get the Second Most Recent Activity](https://leetcode.com/problems/get-the-second-most-recent-activity/)|[Solution](../master/database/_1369.sql) || Hard |
920921
|1364|[Number of Trusted Contacts of a Customer](https://leetcode.com/problems/number-of-trusted-contacts-of-a-customer/)|[Solution](../master/database/_1364.sql) || Medium |
921922
|1355|[Activity Participants](https://leetcode.com/problems/activity-participants/)|[Solution](../master/database/_1355.sql) || Medium |
922923
|1350|[Students With Invalid Departments](https://leetcode.com/problems/students-with-invalid-departments/)|[Solution](../master/database/_1350.sql) || Easy |

database/_1369.sql

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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+
--# Write your MySQL query statement below
47+
--credit: https://leetcode.com/problems/get-the-second-most-recent-activity/discuss/530992/MySQL-solution-184ms-no-subquery
48+
49+
select * from UserActivity
50+
group by username
51+
having count(*) = 1
52+
53+
union all
54+
55+
select u1.*
56+
from UserActivity as u1
57+
left join UserActivity as u2
58+
on u1.username = u2.username and u1.endDate < u2.endDate
59+
group by u1.username, u1.endDate
60+
having count(u2.endDate) = 1

0 commit comments

Comments
 (0)