好友
阅读权限10
听众
最后登录1970-1-1
|
DECLARE @CardList TABLE (id int identity(1,1),SerialNumber nvarchar(16), IsActive INT)
INSERT INTO @CardList(SerialNumber,IsActive)
VALUES('No.B0013206',1),('No.B0013207',0),('No.B0013208',0),('No.B0013209',0),('No.B0013210',0),('No.B0013211',0),('No.B0013212',1),('No.B0013213',0),('No.B0013214',0),('No.B0013215',0),('No.B0013216',0),('No.B0013217',0),('No.B0013218',0),('No.B0013219',0),('No.B0013220',0),('No.B0013221',0),('No.B0013222',0),('No.B0013223',1)
;
with tmp as (
select
ROW_NUMBER() over (
order by
Id asc
) as IdNew,
*
from
@CardList
where
IsActive = 0
)
select
min(SerialNumber)+ '-' + max(SerialNumber) as 号段,
count(id) as 连续数量
from
(
select
a.IdNew,
a.Id,
a.SerialNumber,
case when ISNULL(
replace(a.SerialNumber, 'No.B', ''),
0
)-1 = replace(b.SerialNumber, 'No.B', '')
or replace(b.SerialNumber, 'No.B', '') is null then '连续' else '不连续' end as '连续标志'
from
tmp a
left join tmp b on replace(a.SerialNumber, 'No.B', '') = replace(b.SerialNumber, 'No.B', '')+ 1
) sss
group by
(sss.Id - sss.IdNew) |
|