------一个有意思的题目-----------
2009-12-14 22:08
239 查看
请教各位:
数据记录是这样的
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
*/
相关文章推荐
- 一个关于值类型很有意思的题目
- 分享一个有意思题目--击鼓传花
- 一个很有意思的面试题目
- 看到一个比较有意思的题目,记下来
- 一个有意思的题目
- 分享一个比较有意思的题目
- 一个关于把集合拆分的有意思的小题目(将1到N的连续整数组成的集合划分成两个子集合)
- 一个有意思的逻辑训练题目
- 记录一个有意思的题目
- 一个很有意思公司的题目
- 一个特别有意思的程序,在一本书上的题目,但是书上答案是错的。
- 最近遇到一个比较有意思的题目
- 一个有意思的题目:单循环实现乘法表
- 【题目25】求一个无序数组的第nth大的数
- 一个很有意思的flash游戏,蚂蚁蛋糕保卫战(antbuster)
- flag2在二元树中找出和为某一值的所有路径 题目:输入一个整数和一棵二元树。 从树的根结点开始往下访问一直到叶结点所经过的所有结点形成一条路径。 打印出和与输入整数相等的所有路径。 例如 输入整数
- 题目: 一个数如果恰好等于它的因子之和,这个数被成为”完数”,例如:6=1+2+3.请编程找出1000以内的完数
- python题目-----Python里面如何拷贝一个对象?
- 题目要求:一个人从x年开始庆祝生日,直到死去为止,每过一年就把当年的年龄与以前相加得到一个sum值,求开始过生日的年龄,与寿命
- EOJ - 我决不会TLE (一个智障的题目)