加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MsSql教程 > 正文

一道有趣的题目 转F

发布时间:2020-12-12 14:48:48 所属栏目:MsSql教程 来源:网络整理
导读:? 请教各位: 数据记录是这样的 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 u
?

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读