一道有趣的题目 转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
*/
数据记录是这样的
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
*/
相关文章推荐
- 在CSDN上看到的一道有趣的网易笔试题目--附答案
- 关于byte的一道有趣的题目
- 一道有趣的C#考试题目
- 一道有趣的题目007
- 前几天遇到一道有趣的题目 一座楼有10层,兔子可以跳跃的层数可以为1,2或者3,那么兔子到达10层有多少种跳法并列出各种情况。
- 一道有趣的SQL题目
- 一道有趣的题目
- 一道有趣的题目
- 一道有趣的++运算题目
- 对“一道有趣的题目”的解答
- 一道有趣的题目,看看你的观点是分别开出拿些人?
- 一道有趣的题目
- 一道有趣的题目
- 一道有趣的循环题目
- 一道有趣的题目,看看你的观点是分别开出拿些人?
- 一道有趣的C#考试题目
- 一道有趣题目
- 自家人不认识自家人——考你一道有趣的Javascript小题目
- 一道有趣的笔试题目!
- 一道有趣的C#考试题目