180. Consecutive Numbers
Write a SQL query to find all numbers that appear at least three times consecutively.
+—-+—–+
| Id | Num |
+—-+—–+
| 1 | 1 |
| 2 | 1 |
| 3 | 1 |
| 4 | 2 |
| 5 | 1 |
| 6 | 2 |
| 7 | 2 |
+—-+—–+
+—-+——-+
For example, given the above Logs
table, 1
is the only number that appears consecutively for at least three times.
+—————–+
| ConsecutiveNums |
+—————–+
| 1 |
+—————–+
解答:这道题有点点坑,因为没有说明Id
是否连续,虽然题目默认是连续的。
一开始没有申请题,认为只是提取数量超过3个的,用了下面这个查询语句:
题目的关键在于连续取三行数据,且要保证数目相等,ID
默认连续,语句如下:
等于数据表自身内连接了两次,取数字相同且ID连续3个的记录。