|
| 1 | +# [1890. The Latest Login in 2020](https://leetcode-cn.com/problems/the-latest-login-in-2020) |
| 2 | + |
| 3 | +[English Version](/solution/1800-1899/1890.The%20Latest%20Login%20in%202020/README_EN.md) |
| 4 | + |
| 5 | +## 题目描述 |
| 6 | + |
| 7 | +<!-- 这里写题目描述 --> |
| 8 | + |
| 9 | +<p>Table: <code>Logins</code></p> |
| 10 | + |
| 11 | +<pre> |
| 12 | + |
| 13 | ++----------------+----------+ |
| 14 | + |
| 15 | +| Column Name | Type | |
| 16 | + |
| 17 | ++----------------+----------+ |
| 18 | + |
| 19 | +| user_id | int | |
| 20 | + |
| 21 | +| time_stamp | datetime | |
| 22 | + |
| 23 | ++----------------+----------+ |
| 24 | + |
| 25 | +(user_id, time_stamp) is the primary key for this table. |
| 26 | + |
| 27 | +Each row contains information about the login time for the user with ID user_id. |
| 28 | + |
| 29 | +</pre> |
| 30 | + |
| 31 | +<p> </p> |
| 32 | + |
| 33 | +<p>Write an SQL query to report the <strong>latest</strong> login for all users in the year <code>2020</code>. Do <strong>not</strong> include the users who did not login in <code>2020</code>.</p> |
| 34 | + |
| 35 | +<p>Return the result table <strong>in any order</strong>.</p> |
| 36 | + |
| 37 | +<p>The query result format is in the following example:</p> |
| 38 | + |
| 39 | +<p> </p> |
| 40 | + |
| 41 | +<pre> |
| 42 | + |
| 43 | +Logins table: |
| 44 | + |
| 45 | ++---------+---------------------+ |
| 46 | + |
| 47 | +| user_id | time_stamp | |
| 48 | + |
| 49 | ++---------+---------------------+ |
| 50 | + |
| 51 | +| 6 | 2020-06-30 15:06:07 | |
| 52 | + |
| 53 | +| 6 | 2021-04-21 14:06:06 | |
| 54 | + |
| 55 | +| 6 | 2019-03-07 00:18:15 | |
| 56 | + |
| 57 | +| 8 | 2020-02-01 05:10:53 | |
| 58 | + |
| 59 | +| 8 | 2020-12-30 00:46:50 | |
| 60 | + |
| 61 | +| 2 | 2020-01-16 02:49:50 | |
| 62 | + |
| 63 | +| 2 | 2019-08-25 07:59:08 | |
| 64 | + |
| 65 | +| 14 | 2019-07-14 09:00:00 | |
| 66 | + |
| 67 | +| 14 | 2021-01-06 11:59:59 | |
| 68 | + |
| 69 | ++---------+---------------------+ |
| 70 | + |
| 71 | + |
| 72 | + |
| 73 | +Result table: |
| 74 | + |
| 75 | ++---------+---------------------+ |
| 76 | + |
| 77 | +| user_id | last_stamp | |
| 78 | + |
| 79 | ++---------+---------------------+ |
| 80 | + |
| 81 | +| 6 | 2020-06-30 15:06:07 | |
| 82 | + |
| 83 | +| 8 | 2020-12-30 00:46:50 | |
| 84 | + |
| 85 | +| 2 | 2020-01-16 02:49:50 | |
| 86 | + |
| 87 | ++---------+---------------------+ |
| 88 | + |
| 89 | + |
| 90 | + |
| 91 | +User 6 logged into their account 3 times but only once in 2020, so we include this login in the result table. |
| 92 | + |
| 93 | +User 8 logged into their account 2 times in 2020, once in February and once in December. We include only the latest one (December) in the result table. |
| 94 | + |
| 95 | +User 2 logged into their account 2 times but only once in 2020, so we include this login in the result table. |
| 96 | + |
| 97 | +User 14 did not login in 2020, so we do not include them in the result table. |
| 98 | + |
| 99 | +</pre> |
| 100 | + |
| 101 | +## 解法 |
| 102 | + |
| 103 | +<!-- 这里可写通用的实现逻辑 --> |
| 104 | + |
| 105 | +<!-- tabs:start --> |
| 106 | + |
| 107 | +### **SQL** |
| 108 | + |
| 109 | +<!-- 这里可写当前语言的特殊实现逻辑 --> |
| 110 | + |
| 111 | +```sql |
| 112 | + |
| 113 | +``` |
| 114 | + |
| 115 | +<!-- tabs:end --> |
0 commit comments