File tree 1 file changed +61
-0
lines changed
05_Avanced_Select_and_Joins
1 file changed +61
-0
lines changed Original file line number Diff line number Diff line change
1
+ /*
2
+
3
+ Problem Link -> https://leetcode.com/problems/consecutive-numbers/description/
4
+
5
+ ------------------------------------------------------------- QUESTION -----------------------------------------------------------
6
+
7
+ Table: Logs
8
+
9
+ +-------------+---------+
10
+ | Column Name | Type |
11
+ +-------------+---------+
12
+ | id | int |
13
+ | num | varchar |
14
+ +-------------+---------+
15
+ In SQL, id is the primary key for this table.
16
+ id is an autoincrement column.
17
+
18
+
19
+ Find all numbers that appear at least three times consecutively.
20
+ Return the result table in any order.
21
+ The result format is in the following example.
22
+
23
+
24
+ Example 1:
25
+
26
+ Input:
27
+ Logs table:
28
+ +----+-----+
29
+ | id | num |
30
+ +----+-----+
31
+ | 1 | 1 |
32
+ | 2 | 1 |
33
+ | 3 | 1 |
34
+ | 4 | 2 |
35
+ | 5 | 1 |
36
+ | 6 | 2 |
37
+ | 7 | 2 |
38
+ +----+-----+
39
+
40
+ Output:
41
+ +-----------------+
42
+ | ConsecutiveNums |
43
+ +-----------------+
44
+ | 1 |
45
+ +-----------------+
46
+
47
+ Explanation: 1 is the only number that appears consecutively for at least three times.
48
+
49
+ */
50
+
51
+ -- ----------------------------------------------------------- SOLUTION -----------------------------------------------------------
52
+
53
+ SELECT DISTINCT L1 .Num AS ConsecutiveNums
54
+ FROM Logs L1
55
+ JOIN Logs L2
56
+ ON L1 .Id = L2 .Id + 1
57
+ AND L1 .Num = L2 .Num
58
+ JOIN Logs L3
59
+ ON L1 .Id = L3 .Id + 2
60
+ AND L1 .Num = L3 .Num
61
+
You can’t perform that action at this time.
0 commit comments