当前位置: 代码迷 >> Sql Server >> 急用!怎么查找2个或2个以上的连续号码的记录
  详细解决方案

急用!怎么查找2个或2个以上的连续号码的记录

热度:86   发布时间:2016-04-24 10:37:45.0
急用!如何查找2个或2个以上的连续号码的记录

--> 建立数据表#temp --数据表#temp里有'notext'字段记录(一共有 178448行数据)

if object_id('tempdb.dbo.#temp') is not null drop table #temp
go 
create table #temp([sid] int,[notext] nvarchar(20))
insert #temp

select 111, '01 02 03 04 05 06' union all
select 112, '01 02 03 04 05 18' union all
select 113, '01 02 03 04 18 19' union all
select 114, '01 02 03 07 19 20' union all
select 115, '01 02 03 07 20 21' union all
select 116, '01 02 03 07 21 22' union all
select 117, '01 02 03 07 22 23' union all
select 118, '01 02 03 07 23 24' union all
select 119, '01 02 03 07 24 25' union all
select 120, '01 02 03 07 25 26' union all
select 121, '01 02 03 07 26 27' union all
select 122, '01 02 03 07 27 28' union all
select 123, '01 02 03 07 28 29' union all
select 124, '01 02 03 07 29 30' union all
select 125, '01 02 03 07 30 31' union all
select 126, '01 02 03 07 31 32' union all
select 127, '01 02 03 07 32 33' union all
select 128, '01 02 03 08 09 10' union all
select 129, '01 02 03 08 10 13' union all
select 130, '01 02 04 08 10 12'
-->一下测试代码,是错误的
Select NoText From #temp
 Where (SUBSTRING(notext,16,2)-SUBSTRING(notext,13,2)=1 
 or SUBSTRING(notext,13,2)-SUBSTRING(notext,10,2)=1 
 and SUBSTRING(notext,10,2)-SUBSTRING(notext,7,2)=1 
 and SUBSTRING(notext,7,2)-SUBSTRING(notext,4,2)=1 
 and SUBSTRING(notext,4,2)-SUBSTRING(notext,1,2)=1) 
-->如何分别查找2个、或 3个连续、或4个连续 、或 5个连续、或6个连续号码的记录

举例,执行代码得到结果:
2个连续号码的记录   130, '01 02 04 08 10 12'

3个连续号码的记录   01 02 03 07 19 20
01 02 03 07 20 21
01 02 03 07 21 22
01 02 03 07 22 23
01 02 03 07 23 24
01 02 03 07 24 25
01 02 03 07 25 26
01 02 03 07 26 27
01 02 03 07 27 28
01 02 03 07 28 29
01 02 03 07 29 30
01 02 03 07 30 31
01 02 03 07 31 32
01 02 03 07 32 33
01 02 03 08 09 10
01 02 03 08 10 13

4个连续号码的记录  01 02 03 04 18 19

5个连续号码的记录  01 02 03 04 05 18

6个连续号码的记录  01 02 03 04 05 06


------解决方案--------------------

create table #temp([sid] int,[notext] nvarchar(20))

insert #temp
select 111, '01 02 03 04 05 06' union all
select 112, '01 02 03 04 05 18' union all
select 113, '01 02 03 04 18 19' union all
select 114, '01 02 03 07 19 20' union all
select 115, '01 02 03 07 20 21' union all
select 116, '01 02 03 07 21 22' union all
select 117, '01 02 03 07 22 23' union all
select 118, '01 02 03 07 23 24' union all
select 119, '01 02 03 07 24 25' union all
select 120, '01 02 03 07 25 26' union all
select 121, '01 02 03 07 26 27' union all
select 122, '01 02 03 07 27 28' union all
select 123, '01 02 03 07 28 29' union all
select 124, '01 02 03 07 29 30' union all
select 125, '01 02 03 07 30 31' union all
select 126, '01 02 03 07 31 32' union all
select 127, '01 02 03 07 32 33' union all
select 128, '01 02 03 08 09 10' union all
select 129, '01 02 03 08 10 13' union all
select 130, '01 02 04 08 10 12'

DECLARE @n INT
SET @n=2--2个连续号码的记录,3个则等于3,4个则等于4,以此类推

;WITH a1 AS
(
SELECT a.sid,a.notext,b.notext2,b.notext2-ROW_NUMBER() OVER(PARTITION BY a.sid ORDER BY b.notext2) re
  相关解决方案