34,838
社区成员




--->这个是 缺失范围查询
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([Num] int)
insert [tb]
select 1 union all
select 2 union all
select 4 union all
select 6 union all
select 7 union all
select 8 union all
select 10 union all
select 11 union all
select 13
select cur+1 as startNum,nxt-1 as endNum
from(
select Num as cur,(select MIN(Num) from tb lt1 where t.Num<lt1.Num) as nxt
from tb t
) p
where nxt-cur>1;
----------------------------------------------------------------
-- Author :fredrickhu(小F,向高手学习)
-- Date :2013-10-17 11:32:39
-- Verstion:
-- Microsoft SQL Server 2012 - 11.0.2100.60 (X64)
-- Feb 10 2012 19:39:15
-- Copyright (c) Microsoft Corporation
-- Enterprise Edition: Core-based Licensing (64-bit) on Windows NT 6.1 <X64> (Build 7601: Service Pack 1)
--
----------------------------------------------------------------
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([Num] int)
insert [tb]
select 1 union all
select 2 union all
select 4 union all
select 6 union all
select 7 union all
select 8 union all
select 10 union all
select 11 union all
select 13
--------------开始查询--------------------------
select
rtrim(a.num) AS columus1,(case when min(b.num)!=a.num then rtrim(min(b.num)) else a.num end) AS columus2
from
(select t.num from tb t where not exists(select 1 from tb where num=t.num-1)) a,
(select t.num from tb t where not exists(select 1 from tb where num=t.num+1)) b
where
a.num<=b.num
group by
a.num
----------------结果----------------------------
/* columus1 columus2
------------ -----------
1 2
4 4
6 8
10 11
13 13
(5 行受影响)
*/