您的位置:首页 > 其它

一道有趣的题目 转F

2011-12-19 10:45 267 查看
请教各位:

数据记录是这样的

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

*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: