Skip to content

Commit ab6cb72

Browse files
committed
Upload 159
1 parent 0f24a2c commit ab6cb72

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ Because this project necessitated a framework to enable consistent daily practic
187187
| 156 | [Top Travellers](https://leetcode.com/problems/top-travellers/description/) | [Solution](solutions/156_top_travellers.md) | LeetCode | Easy | |
188188
| 157 | [Rearrange Products Table](https://leetcode.com/problems/rearrange-products-table/description/) | [Solution](solutions/157_rearrange_products_table.md) | LeetCode | Easy | |
189189
| 158 | [Calculate Special Bonus](https://leetcode.com/problems/calculate-special-bonus/description/) | [Solution](solutions/158_calculate_special_bonus.md) | LeetCode | Easy | |
190+
| 159 | [The Latest Login in 2020](https://leetcode.com/problems/the-latest-login-in-2020/description/) | [Solution](solutions/159_the_latest_login_in_2020.md) | LeetCode | Easy | |
190191
<!-- Index End - WARNING: Do not delete or modify this markdown comment. -->
191192
<!--- cSpell:enable --->
192193

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# SQL Everyday \#159
2+
3+
## The Latest Login in 2020
4+
5+
Site: LeetCode\
6+
Difficulty per Site: Easy
7+
8+
## Problem
9+
10+
Write a solution to report the *latest* login for all users in the year `2020`. Do *not* include the users who did not login in `2020`.
11+
12+
Return the result table *in any order*. [[Full Description](https://leetcode.com/problems/the-latest-login-in-2020/description/)]
13+
14+
## Submitted Solution
15+
16+
```sql
17+
-- Submitted Solution
18+
SELECT
19+
user_id
20+
,MAX(time_stamp) AS last_stamp
21+
FROM Logins
22+
WHERE time_stamp BETWEEN '2020-01-01 00:00:00' AND '2020-12-31 23:59:59'
23+
GROUP BY user_id
24+
;
25+
```
26+
27+
## Site Solution
28+
29+
```sql
30+
-- LeetCode Solution
31+
-- Solution 1: Using YEAR() to extract year from the date column and MAX() to find the latest record
32+
SELECT
33+
user_id,
34+
MAX(time_stamp) AS last_stamp
35+
FROM
36+
Logins
37+
WHERE
38+
YEAR(time_stamp) = 2020
39+
GROUP BY 1;
40+
41+
-- Solution 2: Using EXTRACT() to get year from the date column and FIRST_VALUE() to find the latest record
42+
SELECT
43+
DISTINCT user_id,
44+
FIRST_VALUE(time_stamp)OVER(PARTITION BY user_id ORDER BY time_stamp DESC) AS last_stamp
45+
FROM
46+
Logins
47+
WHERE EXTRACT(Year FROM time_stamp) = 2020;
48+
```
49+
50+
## Notes
51+
52+
TBD
53+
54+
## NB
55+
56+
TBD
57+
58+
Go to [Index](../?tab=readme-ov-file#index)\
59+
Go to [Overview](../?tab=readme-ov-file)

0 commit comments

Comments
 (0)