Skip to content

Commit

Permalink
Update 0180.(Medium) Consecutive Numbers.sql
Browse files Browse the repository at this point in the history
  • Loading branch information
haibarawu authored Jun 4, 2020
1 parent 4f8e526 commit d0baf24
Showing 1 changed file with 23 additions and 5 deletions.
28 changes: 23 additions & 5 deletions Database/0180.(Medium) Consecutive Numbers.sql
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
/****************************************************************************************************
0180. Consecutive Numbers
180. Consecutive Numbers
180. 连续出现的数字
Difficulty: Medium
Write a SQL query to find all numbers that appear at least three times consecutively.
编写一个 SQL 查询,查找所有至少连续出现三次的数字。
SQL 架构:
Create table If Not Exists Logs (Id int, Num int)
Truncate table Logs
insert into Logs (Id, Num) values ('1', '1')
insert into Logs (Id, Num) values ('2', '1')
insert into Logs (Id, Num) values ('3', '1')
insert into Logs (Id, Num) values ('4', '2')
insert into Logs (Id, Num) values ('5', '1')
insert into Logs (Id, Num) values ('6', '2')
insert into Logs (Id, Num) values ('7', '2')
+----+-----+
| Id | Num |
+----+-----+
Expand Down Expand Up @@ -37,9 +51,13 @@ FROM (
WHERE Num = lag AND Num = lead


/****************************************************************************************************/


--Method2:
SELECT DISTINCT l1.Num AS ConsecutiveNums
FROM Logs l1, Logs l2, Logs l3
WHERE l1.Id = l2.Id - 1 AND l2.Id = l3.Id - 1
AND l1.Num = l2.Num AND l2.Num = l3.Num
SELECT DISTINCT l1.Num AS ConsecutiveNums
FROM Logs AS l1, Logs AS l2, Logs AS l3
WHERE l1.Id = l2.Id - 1 AND l2.Id = l3.Id - 1
AND l1.Num = l2.Num AND l2.Num = l3.Num


0 comments on commit d0baf24

Please sign in to comment.