现在的位置: 首页 > 综合 > 正文

——一个有意思的题目———–

2013年08月13日 ⁄ 综合 ⁄ 共 567字 ⁄ 字号 评论关闭

请教各位:
数据记录是这样的
1
2
3
4
5
12
17
18
19
20
25
请问sqlserver2000如何显示成这样1-5,12,17-20,25

 

declare @t table(num int)
insert into @t select 1
union all select 2
union all select 3
union all select 4
union all select 5
union all select 12
union all select 17
union all select 18
union all select 19
union all select 20
union all select 25

select
   
rtrim(a.num)+(case when min(b.num)!=a.num then '-'+rtrim(min(b.num)) else '' end)
from
    (
select t.num from @t t where not exists(select 1 from @t where num=t.num-1)) a,
    (
select t.num from @t t where not exists(select 1 from @t where num=t.num+1)) b
where
    a.num
<=b.num
group by
a.num
/*
-------------------------
1-5
12
17-20
25
*/
 

 

 

抱歉!评论已关闭.