File tree Expand file tree Collapse file tree 2 files changed +36
-0
lines changed Expand file tree Collapse file tree 2 files changed +36
-0
lines changed Original file line number Diff line number Diff line change @@ -79,6 +79,7 @@ Have a good contributing!
7979 - [ 2356. Number of Unique Subjects Taught by Each Teacher] ( ./leetcode/easy/2356.%20Number%20of%20Unique%20Subjects%20Taught%20by%20Each%20Teacher.sql )
80802 . [ Medium] ( ./leetcode/medium/ )
8181 - [ 176. Second Highest Salary] ( ./leetcode/medium/176.%20Second%20Highest%20Salary.sql )
82+ - [ 180. Consecutive Numbers] ( ./leetcode/medium/180.%20Consecutive%20Numbers.sql )
8283 - [ 184. Department Highest Salary] ( ./leetcode/medium/184.%20Department%20Highest%20Salary.sql )
8384 - [ 550. Game Play Analysis IV] ( ./leetcode/medium/550.%20Game%20Play%20Analysis%20IV.sql )
8485 - [ 570. Managers with at Least 5 Direct Reports] ( ./leetcode/medium/570.%20Managers%20with%20at%20Least%205%20Direct%20Reports.sql )
Original file line number Diff line number Diff line change 1+ /*
2+ Question 180. Consecutive Numbers
3+ Link: https://leetcode.com/problems/consecutive-numbers/description/?envType=study-plan-v2&envId=top-sql-50
4+
5+ Table: Logs
6+
7+ +-------------+---------+
8+ | Column Name | Type |
9+ +-------------+---------+
10+ | id | int |
11+ | num | varchar |
12+ +-------------+---------+
13+ In SQL, id is the primary key for this table.
14+ id is an autoincrement column starting from 1.
15+
16+
17+ Find all numbers that appear at least three times consecutively.
18+
19+ Return the result table in any order.
20+ */
21+
22+ WITH prev_next AS (
23+ SELECT
24+ id,
25+ LAG(num) OVER(ORDER BY id) AS prev_num,
26+ LEAD(num) OVER(ORDER BY id) AS next_num
27+ FROM Logs
28+ )
29+
30+ SELECT DISTINCT l .num AS ConsecutiveNums
31+ FROM Logs AS l
32+ LEFT JOIN
33+ prev_next AS pn
34+ ON l .id = pn .id
35+ WHERE l .num = pn .prev_num AND pn .prev_num = pn .next_num
You can’t perform that action at this time.
0 commit comments