微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

------一个有意思的题目-----------

请教各位:
数据记录是这样的
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
*/  

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐