不同类型的不连续的最小值

create table #UU1
(
Num
varchar(20)
)
insert into #UU1 select 'a102'
insert into #UU1 select 'a103'
insert into #UU1 select 'a105'
insert into #UU1 select 'a907'
insert into #UU1 select 'a1001'
insert into #UU1 select 'b203'
insert into #UU1 select 'b204'
insert into #UU1 select 'b206'


--1
select *
from #UU1 a
where exists(select 1 from#UU1 where cast(stuff(Num ,1,1,'') as int) = cast(stuff(a.Num ,1,1,'') as int)-1)
and not exists
(
select 1 from #UU1 where cast(stuff(Num ,1,1,'') as int) = cast(stuff(a.Num ,1,1,'') as int)+1)

--2

;with hgo as
(
select substring(Num,1,1) num,substring(Num,2,len(num)-1) cnt,
row_number()
over(partition by substring(Num,1,1) order by Num asc) as rank
from #UU1
),
h
as
(
select * from hgo h where not exists(select * from hgo where num=h.num and rank=h.rank-1 and cnt<>h.cnt-1)
),
g
as
(
select num,max(cnt) cnt from h group by num
)
select ltrim(num+ltrim(cnt)) from g

----------------------
a103
b204

(
2 行受影响)

你可能感兴趣的:(类型)