DB2中如何使用SQL查找非连续数值
来源:互联网 发布:淘宝透明内衣买家晒图 编辑:程序博客网 时间:2024/04/29 02:55
问题:
假设有一个表,一列int字段存放数字,值如: 1 2 3 4 5 8 9 10 20 21 22
其中1 2 3 4 5是连续的,8 9 10是连续的,20 21 22是连续的,请问如何用sql找出每个连续序列中的第一个数字,也就是找出1、8、20三个数字。
连续是指最少两个数字是相邻的,就可以看作为一个连续序列。
总的数据量不大,几万条到十几万条不等。这个操作是对已经收集到的数据做分析过滤用的,所以性能不用考虑太多。
初始化:
db2 create table test30 (col1 int)
以下为每种方法和该方法使用的SQL语句的执行计划
方法1:
select col1 from test30 tt where tt.col1+1 in (select t.col1 from test30 t ) and tt.col1-1 not in (select t.col1 from test30 t )
方法2:
select col2+1 from ( select col2 from (select col1-1 col2 from test30) except select col1 col2 from test30 )
方法3:
with t ( n1,n2) as ( select col1, lag(col1,1) OVER(ORDER BY col1) AS col2 from test30 ) select n1 from t where (n2 is null ) or ( n1- n2 > 1)
方法4:
with tmp (num) as ( SELECT col1 FROM test30 ), tmp2 (num, next_num) as ( select num, coalesce(min(num) over(order by num rows between 1 following and 1 following), num+1) next_num from tmp) select num from ( select num, next_num-min(next_num) over(order by next_num rows between 1 preceding and 1 preceding) gap from tmp2 where next_num-num = 1) a where coalesce(gap,2) >1
- DB2中如何使用SQL查找非连续数值
- 查找某张表中未使用到的连续数值
- 数据库中如何使用SQL查询连续号码段
- java如何将集合中连续的数值间隔开
- DB2 命令行中如何执行sql脚本
- DB2的SQL中如何计算时间差
- SQL Server 2005中删除了数据库表中自增量(标识列)后,再增加如何保持数值连续?
- db2中使用oracle 的一些sql
- DB2查找重复记录SQL
- 如何使用SQL语句查询连续编号中缺失的编号?
- 在Linux的Vim编辑器中,如何使用非贪婪匹配模式查找
- DB2数值函数简介及使用
- DB2数值函数简介及使用
- 二维数组中查找数值
- 如何使用DB2条件处理器处理SQL错误
- DB2中跟踪SQL
- DB2 SQL/XML使用
- 在 DB2 SQL 存储过程中使用 XML
- 自己写远程控制软件之捕捉屏幕
- java中System.currentTimeMillis()和System.nanoTime()区别1
- Codejam 2009 qualification round question C
- Makefile 中:= ?= += =的区别
- 2013 前20名榜单排行榜
- DB2中如何使用SQL查找非连续数值
- 判别式模型与生成式模型
- oracle profile总结
- java格式化字符串
- android 调用其他程序
- 【小蒙淘金】1.31早评-黄金白银行情分析及部分操作建议
- Codejam 2010 qualification round question C
- 虚拟机与主机通信(无网线)
- git的简单使用(1)