sql - SQL (Redshift) - 查找连续数字
问题描述
假设我有一个下表:
ID NUM
1 1
2 1
3 1
4 2
5 3
6 1
我想找出连续出现 3 次的数字。所以在这个例子中,输出是 1。
当数据中有更多行时,如何使用常规 SQL(而不是 PL/SQL)来实现这一点?
谢谢!
解决方案
这应该有效。这是sqlfiddle
select
distinct num as ConsecutiveNums
from
(
select
num,
count(*) as total
from
(
select
num,
id - row_number() over (partition by num order by id) as rnk
from myTable
) t
group by
num,
rnk
) tt
where total > 2
推荐阅读
- ruby-on-rails - 如何使用 rspec 测试 API 路由中的方法?
- reactjs - 我如何在 react.js 上导出一些东西
- python - 具有历史数据和未来数据的 Python CSV - 需要分组和计算值
- vbscript - 如何修复此代码上的预期语句错误?
- redirect - 使用 RoutablePageMixin 显示给定类别
- go - 上传视频时出现 403 速率限制超出错误
- python - Pandas groupby - 每组单独计算唯一的列
- python - 如何在带有 xonsh 的 find 命令上使用 -exec?
- aws-serverless - 是否可以在 AWS 无服务器应用程序模板文件中指定堆栈名称?
- powershell - 如何在powershell中组合两个具有不同列标题的CSV文件