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.

# Write your MySQL query statement below
SELECT DISTINCT Num FROM (
SELECT Num,
@cnt := IF(@prevNum = Num, @cnt, 0),
@cnt := @cnt + 1 as Cnt,
@prevNum := Num
FROM Logs s, (SELECT @cnt := 0) r, (SELECT @prevNum := NULL) p
ORDER BY Id ASC
) t where Cnt >= 3 ;

  

LeetCode Database: Consecutive Numbers的更多相关文章

  1. [LeetCode] 829. Consecutive Numbers Sum 连续数字之和

    Given a positive integer N, how many ways can we write it as a sum of consecutive positive integers? ...

  2. [LeetCode#180]Consecutive Numbers

    Write a SQL query to find all numbers that appear at least three times consecutively. +----+-----+ | ...

  3. [LeetCode] Consecutive Numbers 连续的数字 --数据库知识(mysql)

    1. 题目名称   Consecutive Numbers 2 .题目地址 https://leetcode.com/problems/consecutive-numbers/ 3. 题目内容 写一个 ...

  4. leetcode Database3(Nth Highest Salary<—>Consecutive Numbers<—>Department Highest Salary)

    一.Nth Highest Salary Write a SQL query to get the nth highest salary from the Employee table. +----+ ...

  5. 【leetcode】1296. Divide Array in Sets of K Consecutive Numbers

    题目如下: Given an array of integers nums and a positive integer k, find whether it's possible to divide ...

  6. LeetCode Database题解

    175. Combine Two Tables 使用外连接即可. # Write your MySQL query statement below select FirstName, LastName ...

  7. [LeetCode] All questions numbers conclusion 所有题目题号

    Note: 后面数字n表明刷的第n + 1遍, 如果题目有**, 表明有待总结 Conclusion questions: [LeetCode] questions conclustion_BFS, ...

  8. LeetCode——Longest Consecutive Sequence

    LeetCode--Longest Consecutive Sequence Question Given an unsorted array of integers, find the length ...

  9. LeetCode——Find All Numbers Disappeared in an Array

    LeetCode--Find All Numbers Disappeared in an Array Question Given an array of integers where 1 ≤ a[i ...

随机推荐

  1. mysql命令分类(DML、DDL、DCL)

    DML:数据操作语言(操作数据) SELECT - 从数据库表中获取数据 UPDATE - 更新数据库表中的数据 DELETE - 从数据库表中删除数据 INSERT INTO - 向数据库表中插入数 ...

  2. 深入理解Java内存模型(五)——锁

    锁的释放-获取建立的happens before 关系 锁是java并发编程中最重要的同步机制.锁除了让临界区互斥执行外,还可以让释放锁的线程向获取同一个锁的线程发送消息. 下面是锁释放-获取的示例代 ...

  3. [51NOD]大数加法(模拟)

    题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1005 要处理符号,还要特别注意0和连续进位等情况.偷懒使用st ...

  4. java socket编程基础

    1. [代码]读操作Runable 1 package com.hrd.test.socket; import java.io.BufferedReader; import java.io.IOExc ...

  5. Mybatis 插入与批量插入以及多参数批量删除

    实体类: import java.io.Serializable; public class AttachmentTable implements Serializable { private sta ...

  6. VS2015新功能

    今天有幸参加了微软的 Visual Studio Dev Day,趁还没有忘记今天的学习内容. 先把这些内容记录下来,如果有其他人也参加此次交流活动,请补充完善. VS2015新功能 1,Roslyn ...

  7. Strom Topology执行分析:worker数,Bolt实例数,executor数,task数

    在创建Storm的Topology时,我们通常使用如下代码:builder.setBolt("cpp", new CppBolt(), 3).setNumTasks(5).none ...

  8. BZOJ 4571 美味

    又一部SCOI血泪史.... 唉. 就是在这棵树上一遍又一遍跑嘛. 以后不要直接求答案啊.要最后再异或起来. 要学习简单的代码风格. #include<iostream> #include ...

  9. Android Terminal telnet windows

    /******************************************************************************************** * Andr ...

  10. wxWidgets进度条

    #include <wx/wx.h> #include <wx/progdlg.h> class myApp : public wxApp { public: bool OnI ...