sql-leetcode Consecutive Numbers

Posted 哈哈哈

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sql-leetcode Consecutive Numbers相关的知识,希望对你有一定的参考价值。

 

开始想 用 group 把Num都聚在一起

-- Group By语句从英文的字面意义上理解就是“根据(by)一定的规则进行分组(Group)”。
--它的作用是通过一定的规则将一个数据集划分成若干个小的区域,然后针对若干个小区域进行数据处理。但是group by是先排序后分组;

每个部门有多少人:

 

select DepartmentId as \'部门名称\', count(*) as \'个数\' from basicDepartment group by DepartmentId

  

所以不能使用group by ,将表复制3份进行比较:

select(Select DISTINCT l1.Num 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) as ConsecutiveNums;

  

 

以上是关于sql-leetcode Consecutive Numbers的主要内容,如果未能解决你的问题,请参考以下文章

128. Longest Consecutive Sequence

485. Max Consecutive Ones

LeetCode Max Consecutive Ones

LeetCode Split Array into Consecutive Subsequences

LC.298.Binary Tree Longest Consecutive Sequence

Longest Consecutive Sequence