diff --git a/solution/1300-1399/1369.Get the Second Most Recent Activity/README.md b/solution/1300-1399/1369.Get the Second Most Recent Activity/README.md index c671068c3a345..ba19b0dbb2277 100644 --- a/solution/1300-1399/1369.Get the Second Most Recent Activity/README.md +++ b/solution/1300-1399/1369.Get the Second Most Recent Activity/README.md @@ -67,7 +67,18 @@ Bob 只有一条记录,我们就取这条记录 ### **SQL** ```sql - +SELECT + username, + activity, + startdate, + enddate +FROM (SELECT + *, + RANK() OVER (PARTITION BY username ORDER BY startdate DESC) rk, + COUNT(username) OVER (PARTITION BY username) AS cnt +FROM UserActivity) a +WHERE a.rk = 2 +OR a.cnt = 1; ``` diff --git a/solution/1300-1399/1369.Get the Second Most Recent Activity/README_EN.md b/solution/1300-1399/1369.Get the Second Most Recent Activity/README_EN.md index c8dfd2705c9cf..a99fa9488aee6 100644 --- a/solution/1300-1399/1369.Get the Second Most Recent Activity/README_EN.md +++ b/solution/1300-1399/1369.Get the Second Most Recent Activity/README_EN.md @@ -63,7 +63,18 @@ Bob only has one record, we just take that one. ### **SQL** ```sql - +SELECT + username, + activity, + startdate, + enddate +FROM (SELECT + *, + RANK() OVER (PARTITION BY username ORDER BY startdate DESC) rk, + COUNT(username) OVER (PARTITION BY username) AS cnt +FROM UserActivity) a +WHERE a.rk = 2 +OR a.cnt = 1; ``` diff --git a/solution/1300-1399/1369.Get the Second Most Recent Activity/Solution.sql b/solution/1300-1399/1369.Get the Second Most Recent Activity/Solution.sql new file mode 100644 index 0000000000000..a118932752ce4 --- /dev/null +++ b/solution/1300-1399/1369.Get the Second Most Recent Activity/Solution.sql @@ -0,0 +1,12 @@ +SELECT + username, + activity, + startdate, + enddate +FROM (SELECT + *, + RANK() OVER (PARTITION BY username ORDER BY startdate DESC) rk, + COUNT(username) OVER (PARTITION BY username) AS cnt +FROM UserActivity) a +WHERE a.rk = 2 +OR a.cnt = 1;