筛选 前后 不连续 数据

qqzeng-ip 2012-05-28 07:12:19
表 tb (int id) 连续定义: 就是 (前面一个+1等于后面那个 前提是 以连续的组为单位)
1
2
3
4
1 选
1
2
3
...
7
8
这样第五个1 为不连续数据 选出来 (上面 1-4和下面 1-8)为连续

1
2
3
4
5
1
2
3
...
7
8
这样为连续数据 上面1-5 下面1-7 都为连续 没有要选的


1
2
3
1 选
1 选
1 选
1
2
3
4
选出来为 第四个 1, 第五个 1 第六个 1 不连续 (前面1-3 连续,后面1-4连续)

...全文
313 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
唐诗三百首 2012-05-28
  • 打赏
  • 举报
回复

create table zen(id int)

insert into zen(id)
select 1 union all
select 2 union all
select 3 union all
select 1 union all
select 1 union all
select 1 union all
select 1 union all
select 2 union all
select 3 union all
select 4


with t as
(select row_number() over(order by getdate()) rn,
id from zen
)
select a.id
from t a
left join t b on a.rn=b.rn+1
left join t c on a.rn=c.rn-1
where a.id-isnull(b.id,0)<>1
and isnull(c.id,0)-a.id<>1

/*
id
-----------
1
1
1

(3 row(s) affected)
*/
  • 打赏
  • 举报
回复

--缺失范围和现有范围(也称间断和孤岛问题)
--1、缺失范围(间断)
/*
收集人:TravyLee
时间:2012-03-25
如有引用,请标明“此内容源自MSSQL2008技术内幕之T-SQL”
*/
/*
求解间断问题有几种方法,小弟我选择性能较高的三种(使用游标的方法省略
有兴趣不全的大哥大姐请回复)
---------------------------------------------------------------------
间断问题的解决方案1;使用子查询
step 1:找到间断之前的值,每个值增加一个间隔
step 2:对于没一个间断的起点,找出序列中现有得值,再减去一个间隔
本人理解为:找到原数据表中的值加一减一是否存在,若有不妥,望纠正
生成测试数据:
go
if object_id('tbl')is not null
drop table tbl
go
create table tbl(
id int not null
)
go
insert tbl
values(2),(3),(11),(12),(13),(27),(33),(34),(35),(42)
要求:找到上表数据中的不存在的id的范围,
--实现输出结果:
/*
开始范围 结束范围
4 10
14 26
28 32
36 41
*/
按照每个步骤实现:
step 1:找到间断之前的值,每个值增加一个间隔
我们可以清楚的发现,要找的间断范围的起始值实际上就是我们
现有数据中的某些值加1后存不存在现有数据表中的问题,通过
子查询实现:

select id+1 as start_range from tbl as a
where not exists(select 1 from tbl as b
where b.id=a.id+1)and id<(select max(id) from tbl)
--此查询语句实现以下输出:
/*
start_range
4
14
28
36
*/
step 2:对于没一个间断的起点,找出序列中现有得值,再减去一个间隔

select id+1 as start_range,(select min(b.id) from tbl as b
where b.id>a.id)-1 as end_range
from tbl a where not exists(select 1 from tbl as b
where b.id=a.id+1)
and id<(select max(id) from tbl)
--输出结果:
/*
start_range end_range
4 10
14 26
28 32
36 41
*/
通过以上的相关子查询我们实现了找到原数据表中的间断范围。
而且这种方式的效率较其他方式有绝对的优势


间断问题的解决方案2;使用子查询(主意观察同1的区别)
step 1:对每个现有的值匹配下一个现有的值,生成一对一对的当
前值和下一个值
step 2:只保留下一个值减当前值大于1的间隔值对
step 3:对剩下的值对,将每个当前值增加1个间隔,将每个下一
个值减去一个间隔

--转换成T-SQL语句实现:
--step 1:
select id as cur,(select min(b.id) from tbl b where
b.id>a.id) as nxt from tbl a
--此子查询生成的结果:
/*
cur nxt
2 3
3 11
11 12
12 13
13 27
27 33
33 34
34 35
35 42
42 NULL
*/
step 2 and step 3:
select cur+1 as start_range,nxt-1 as end_range
from (select id as cur,(select min(b.id) from tbl b
where b.id>a.id) as nxt from tbl a ) as d
where nxt-cur>1
--生成结果:
/*
start_range end_range
4 10
14 26
28 32
36 41
*/
间断问题的解决方案3;使用排名函数实现

此种方法与第二种类似,这里我一步实现:

;with c as
(
select id,row_number()over(order by id) as rownum
from tbl
)
select cur.id+1 as strat_range,nxt.id-1 as end_range
from c as cur join c as nxt
on nxt.rownum=cur.rownum+1
where nxt.id-cur.id>1

--输出结果:
/*
strat_range end_range
4 10
14 26
28 32
36 41
*/

*/
--2、现有范围(孤岛)
/*
以上测试数据,试下如下输出:
/*
开始范围 结束范围
2 3
11 13
27 27
33 35
42 42
*/
和间断问题一样,孤岛问题也有集中解决方案,这里也只介绍三种
省略了用游标的实现方案:

孤岛问题解决方案1:使用子查询和排名计算
step 1:找出间断之后的点,为他们分配行号(这是孤岛的起点)
step 2:找出间断之前的点,为他们分配行号(这是孤岛的终点)
step 3:以行号相等作为条件,匹配孤岛的起点和终点

--实现代码:
with startpoints as
(
select id,row_number()over(order by id) as rownum
from tbl as a where not exists(
select 1 from tbl as b where b.id=a.id-1)
/*
此查询语句单独运行的结果:
id rownum
2 1
11 2
27 3
33 4
42 5
*/
),
endpoinds as
(
select id,row_number()over(order by id) as rownum
from tbl as a where not exists(
select 1 from tbl as b where b.id=a.id+1)
/*
此查询语句单独运行的结果:
id rownum
3 1
13 2
27 3
35 4
42 5
*/
)
select s.id as start_range,e.id as end_range
from startpoints as s
inner join endpoinds as e
on e.rownum=s.rownum
--运行结果:
/*
start_range end_range
2 3
11 13
27 27
33 35
42 42
*/

孤岛问题解决方案2:使用基于子查询的组标识符

--直接给出代码:

with d as
(
select id,(select min(b.id) from tbl b where b.id>=a.id
and not exists (select * from tbl c where c.id=b.id+1)) as grp
from tbl a
)
select min(id) as start_range,max(id) as end_range
from d group by grp
/*
start_range end_range
2 3
11 13
27 27
33 35
42 42
*/


孤岛问题解决方案3:使用基于子查询的组标识符:

step 1:按照id顺序计算行号:
select id ,row_number()over(order by id) as rownum from tbl
/*
id rownum
2 1
3 2
11 3
12 4
13 5
27 6
33 7
34 8
35 9
42 10
*/
step 2:生成id和行号的差:
select id,id-row_number()over(order by id) as diff from tbl
/*
id diff
2 1
3 1
11 8
12 8
13 8
27 21
33 26
34 26
35 26
42 32
*/
这里解释一下这样做的原因;
因为在孤岛范围内,这两个序列都以相同的时间间隔来保持增长,所以
这时他们的差值保持不变。只要遇到一个新的孤岛,他们之间的差值就
会增加。这样做的目的为何,第三步将为你说明。
step 3:分别取出第二个查询中生成的相同的diff的值的最大id和最小id
with t as(
select id,id-row_number()over(order by id) as diff from tbl
)
select min(id) as start_range,max(id) as end_range from t
group by diff
/*
start_range end_range
2 3
11 13
27 27
33 35
42 42
*/

求孤岛问题,低三种方法效率较前两种较高,具有比较强的技巧性
希望在实际运用中采纳。
*/
  • 打赏
  • 举报
回复

--TravyLee生成测试数据:
if OBJECT_ID('test')is not null
drop table test
go
create table test(
value int
)
go
insert test
select 1 union all
select 2 union all
select 3 union all
select 4 union all
select 1 union all
select 1 union all
select 2 union all
select 3 union all
select 1 union all
select 1 union all
select 1 union all
select 1 union all
select 2 union all
select 3 union all
select 3 union all
select 2
go

with t
as(
select value-ROW_NUMBER()over(order by (select 1)) as px,
value from test
)
select startrange as value from(
select px,MIN(value) as startrange,max(value) as endrange from t
group by px)b
where startrange=endrange
order by startrange
/*
value
--------
1
1
1
1
2
3
*/

--典型的求孤岛问题
数据分析,数据科学及AI算法是当前最热门的职业。这些职业有着共同的特点:面向数字的,针对编程的以及采取分析手段的。 这些当代热点特性使得在就业市场上对以上职位需求激增也就不足为奇了。但是,市场上提供这方面的大型综合的培训课程是有限,如果说有,大多是知识范围狭窄且非综合性的,而且大多培训都缺乏方法论与实务结合。一般的情况是讲师讲述某种语言的一堆代码,学生听完后甚至连使用方法及代码的前提都不清楚,更别提实际应用场景了。这里,掌握一门数据分析软件本身没错,但仅通过单一的编程培训很难获得聘用为数据分析师或数据科学家所需的技能。那我的解决方案是什么呢?首先,我把所有数据分析中的典型问题都归类总结出来,再结合相应的实际问题,数据以及案例,同时采用世界上最流行的两种数据分析软件:PYTHON 和 SAS去解决这些问题,并将这些解决方法传授给学生。学生在完成培训后更重要的收获是知道每一问题从产生直至解决的前因后果和应用场景,这是因为我在每一课程章节最前都会交代方法论,知识要点及应用场合。SAS和PYTHON可以一起学吗?当然可以。因为我就是这样做到的。具体步骤是,我在课程当中安排了一系列主题,然后使用两种编程语言解决同样的问题。我总结出这样做的好处是边学习边比较,最后在不知不觉当中掌握了两门语言的精华和数据分析的通用方法或模式。过程虽有点长,但十分有趣。最后,为了巩固已学的知识和技能,我还专门安排了针对PYTHON 和 SAS的中小型项目及详细代码讲解。另外,课程当中使用的全部编程代码及数据文件都将免费地提供给注册的学生。

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧