如何生成号码段

Changefish 2009-02-24 02:06:13
有如下数字:
100001
100002
100003
100004
100008
100012
100013
100014
100023
100030
100031
100032
如何将这些数字生成连续号码段表示:
100001-100004,100008,100012-100014,100023,100030-100032
谢谢
...全文
215 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
swwei2002 2009-02-24
  • 打赏
  • 举报
回复

select ltrim(min(col))+'-'+ltrim(max(col)) as [begin-end]
from
(select col,col+row_number() over(order by col desc) as ttl from tb) t
group by t.ttl

和楼上几位思路不太一样,楼主可以参考一下
肥龙上天 2009-02-24
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 josy 的回复:]
SQL code---测试数据---
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([col] int)
insert [tb]
select 100001 union all
select 100002 union all
select 100003 union all
select 100004 union all
select 100008 union all
select 100012 union all
select 100013 union all
select 100014 union all
select 100023 union all
select 100030 union all
select 100031 union all
se…
[/Quote]


树人兄弟的这个语句实在是太经典了,暂时没发现比这更精炼的写法,收藏了
swwei2002 2009-02-24
  • 打赏
  • 举报
回复
学习
dobear_0922 2009-02-24
  • 打赏
  • 举报
回复
--> By dobear_0922(小熊) 2009-02-24 14:18:06
--> 测试数据:[Numbers]
if object_id('[Numbers]') is not null drop table [Numbers]
create table [Numbers]([Num] int)
insert [Numbers]
select 100001 union all
select 100002 union all
select 100003 union all
select 100004 union all
select 100008 union all
select 100012 union all
select 100013 union all
select 100014 union all
select 100023 union all
select 100030 union all
select 100031 union all
select 100032

--SQL2005
select Info=stuff((
select ','+MN from
(
select MN=rtrim(M) + case when M<Max(Num) then '-'+rtrim(Max(Num)) else '' end
from (select Num, M=(select Max(Num) from [Numbers] b where Num<=a.Num
and not exists(select * from [Numbers] where Num=b.Num-1))
from [Numbers] a) T
group by M )TT
for xml path('')), 1,1,'')

/*
Info
-----------------------------------------------------------
100001-100004,100008,100012-100014,100023,100030-100032

(1 行受影响)

*/

drop table [Numbers]
dobear_0922 2009-02-24
  • 打赏
  • 举报
回复
最好不要用游标,如果是SQL2005,连变量都不需要用,,,
dobear_0922 2009-02-24
  • 打赏
  • 举报
回复
--> By dobear_0922(小熊) 2009-02-24 14:18:06
--> 测试数据:[Numbers]
if object_id('[Numbers]') is not null drop table [Numbers]
create table [Numbers]([Num] int)
insert [Numbers]
select 100001 union all
select 100002 union all
select 100003 union all
select 100004 union all
select 100008 union all
select 100012 union all
select 100013 union all
select 100014 union all
select 100023 union all
select 100030 union all
select 100031 union all
select 100032
declare @info varchar(1000)
select @info=isnull(@info+',', '')+ rtrim(M)
+ case when M<N then '-'+rtrim(N) else '' end
from (
select M, max(Num) as N
from (select Num, M=(select Max(Num) from [Numbers] b where Num<=a.Num
and not exists(select * from [Numbers] where Num=b.Num-1))
from [Numbers] a) T
group by M ) TT

select Info=@info
/*
Info
-----------------------------------------------------------
100001-100004,100008,100012-100014,100023,100030-100032

(1 行受影响)

*/

drop table [Numbers]
htl258_Tony 2009-02-24
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 josy 的回复:]
SQL code---测试数据---
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([col] int)
insert [tb]
select 100001 union all
select 100002 union all
select 100003 union all
select 100004 union all
select 100008 union all
select 100012 union all
select 100013 union all
select 100014 union all
select 100023 union all
select 100030 union all
select 100031 union all
se…
[/Quote]顶
oopp1234567890 2009-02-24
  • 打赏
  • 举报
回复
循环,游标都可以
htl258_Tony 2009-02-24
  • 打赏
  • 举报
回复
我想也是要游标
ljhcy99 2009-02-24
  • 打赏
  • 举报
回复
存储过程,游标,判断以下数字是否连续, 循环处理
百年树人 2009-02-24
  • 打赏
  • 举报
回复
---测试数据---
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([col] int)
insert [tb]
select 100001 union all
select 100002 union all
select 100003 union all
select 100004 union all
select 100008 union all
select 100012 union all
select 100013 union all
select 100014 union all
select 100023 union all
select 100030 union all
select 100031 union all
select 100032

---查询---
select col,tid=identity(int,1,1) into #1 from [tb] t where not exists(select * from tb where col=t.col-1)
select col,tid=identity(int,1,1) into #2 from [tb] t where not exists(select * from tb where col=t.col+1)

declare @result varchar(1000)
select
@result=isnull(@result+',','')+
case
when a.col=b.col then ltrim(a.col)
else ltrim(a.col)+'-'+ltrim(b.col)
end
from #1 a,#2 b
where a.tid=b.tid

select @result

---结果---

------------------------------------------------------------------------------
100001-100004,100008,100012-100014,100023,100030-100032

(所影响的行数为 1 行)
sdhdy 2009-02-24
  • 打赏
  • 举报
回复
估计得用游标实现了。
标题付费自习室管理系统的设计与实现研究AI更换标题第1章引言介绍付费自习室管理系统的研究背景、意义、国内外发展现状及论文创新点。1.1研究背景与意义阐述付费自习室兴起背景及管理系统的重要性。1.2国内外发展现状分析国内外付费自习室及其管理系统的发展情况。1.3论文研究方法及创新点概述本文采用的研究方法及系统设计的创新之处。第2章相关理论总结和评述与付费自习室管理系统相关的现有理论。2.1管理信息系统理论介绍管理信息系统的基本概念、构成及作用。2.2用户需求分析理论阐述用户需求分析的方法和步骤。2.3系统设计理论介绍系统设计的原则、方法和流程。第3章付费自习室管理系统需求分析详细描述付费自习室管理系统的功能需求和非功能需求。3.1功能需求分析分析用户管理、座位预订、费用结算等核心功能需求。3.2非功能需求分析阐述系统性能、安全性、易用性等非功能需求。3.3用户调研与反馈介绍用户调研方法及收集到的用户反馈意见。第4章付费自习室管理系统设计详细介绍付费自习室管理系统的设计方案和实现过程。4.1系统架构设计给出系统的整体架构、模块划分及交互流程。4.2数据库设计设计数据库结构,包括表结构、字定义及关系。4.3界面设计展示系统界面设计,包括布局、色彩搭配及交互元素。第5章付费自习室管理系统实现与测试阐述付费自习室管理系统的实现过程和测试结果。5.1系统实现技术介绍系统开发所采用的技术栈和工具。5.2系统测试方法阐述系统测试的方法、步骤及测试用例设计。5.3系统测试结果与分析展示系统测试结果,包括功能测试、性能测试等,并进行分析。第6章结论与展望总结本文的研究成果,并对未来研究方向进行展望。6.1研究结论概括付费自习室管理系统的设计成果及实施效果。6.2展望指出系统存在的不足及未来改进方向,提出后续研究建议。
标题宾馆客房管理系统的设计与实现研究AI更换标题第1章引言介绍宾馆客房管理系统的研究背景、研究意义、国内外研究现状、论文方法及创新点。1.1研究背景与意义阐述宾馆客房管理系统对提升管理效率和服务质量的重要性。1.2国内外研究现状分析国内外宾馆客房管理系统的研究现状与发展趋势。1.3研究方法及创新点概述本文采用的研究方法及系统设计的创新之处。第2章相关理论介绍宾馆客房管理系统设计涉及的相关理论和技术。2.1数据库管理理论阐述数据库设计的基本原理和管理方法。2.2软件开发理论介绍软件开发的基本流程和项目管理方法。2.3用户界面设计理论讨论用户界面设计原则和用户体验优化方法。第3章宾馆客房管理系统需求分析详细分析宾馆客房管理系统的功能需求和性能需求。3.1功能需求分析列举系统应具备的主要功能,如预订、入住、退房等。3.2性能需求分析分析系统在响应时间、并发处理等方面的性能要求。3.3用户需求分析从用户角度出发,分析用户对系统的期望和需求。第4章宾馆客房管理系统设计详细介绍系统的整体架构和各个模块的设计方案。4.1系统架构设计给出系统的整体架构图,包括前端、后端和数据库等部分。4.2数据库设计详细介绍数据库的设计方案,包括表结构、字设置等。4.3模块设计分别介绍各个模块的设计思路和功能实现方法。第5章宾馆客房管理系统实现与测试介绍系统的实现过程和测试方法,验证系统的功能性和性能。5.1系统实现阐述系统的开发环境、开发工具和实现步骤。5.2系统测试介绍系统的测试方法、测试用例和测试结果。5.3性能优化根据测试结果,对系统进行性能优化和改进。第6章结论与展望总结本文的研究成果,并对未来研究方向进行展望。6.1研究结论概括本文的主要研究成果和系统的实际应用效果。6.2展望指出系统存在的不足之处和未来改进的方向。

22,298

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧