22,300
社区成员




select ltrim(min(col))+'-'+ltrim(max(col)) as [begin-end]
from
(select col,col+row_number() over(order by col desc) as ttl from tb) t
group by t.ttl
树人兄弟的这个语句实在是太经典了,暂时没发现比这更精炼的写法,收藏了
--> By dobear_0922(小熊) 2009-02-24 14:18:06
--> 测试数据:[Numbers]
if object_id('[Numbers]') is not null drop table [Numbers]
create table [Numbers]([Num] int)
insert [Numbers]
select 100001 union all
select 100002 union all
select 100003 union all
select 100004 union all
select 100008 union all
select 100012 union all
select 100013 union all
select 100014 union all
select 100023 union all
select 100030 union all
select 100031 union all
select 100032
--SQL2005
select Info=stuff((
select ','+MN from
(
select MN=rtrim(M) + case when M<Max(Num) then '-'+rtrim(Max(Num)) else '' end
from (select Num, M=(select Max(Num) from [Numbers] b where Num<=a.Num
and not exists(select * from [Numbers] where Num=b.Num-1))
from [Numbers] a) T
group by M )TT
for xml path('')), 1,1,'')
/*
Info
-----------------------------------------------------------
100001-100004,100008,100012-100014,100023,100030-100032
(1 行受影响)
*/
drop table [Numbers]
--> By dobear_0922(小熊) 2009-02-24 14:18:06
--> 测试数据:[Numbers]
if object_id('[Numbers]') is not null drop table [Numbers]
create table [Numbers]([Num] int)
insert [Numbers]
select 100001 union all
select 100002 union all
select 100003 union all
select 100004 union all
select 100008 union all
select 100012 union all
select 100013 union all
select 100014 union all
select 100023 union all
select 100030 union all
select 100031 union all
select 100032
declare @info varchar(1000)
select @info=isnull(@info+',', '')+ rtrim(M)
+ case when M<N then '-'+rtrim(N) else '' end
from (
select M, max(Num) as N
from (select Num, M=(select Max(Num) from [Numbers] b where Num<=a.Num
and not exists(select * from [Numbers] where Num=b.Num-1))
from [Numbers] a) T
group by M ) TT
select Info=@info
/*
Info
-----------------------------------------------------------
100001-100004,100008,100012-100014,100023,100030-100032
(1 行受影响)
*/
drop table [Numbers]
---测试数据---
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([col] int)
insert [tb]
select 100001 union all
select 100002 union all
select 100003 union all
select 100004 union all
select 100008 union all
select 100012 union all
select 100013 union all
select 100014 union all
select 100023 union all
select 100030 union all
select 100031 union all
select 100032
---查询---
select col,tid=identity(int,1,1) into #1 from [tb] t where not exists(select * from tb where col=t.col-1)
select col,tid=identity(int,1,1) into #2 from [tb] t where not exists(select * from tb where col=t.col+1)
declare @result varchar(1000)
select
@result=isnull(@result+',','')+
case
when a.col=b.col then ltrim(a.col)
else ltrim(a.col)+'-'+ltrim(b.col)
end
from #1 a,#2 b
where a.tid=b.tid
select @result
---结果---
------------------------------------------------------------------------------
100001-100004,100008,100012-100014,100023,100030-100032
(所影响的行数为 1 行)