求整型字段间的差距值

XiaoCsharp 2003-09-26 10:59:58
有没有一种方法求出整型字段间的间距值
...全文
69 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
bitfubin 2003-09-26
  • 打赏
  • 举报
回复

要的是不是这。。
select 整型字段1,整型字段2,整型字段1-整型字段2 as 差值 from tablename
伍子V5 2003-09-26
  • 打赏
  • 举报
回复
假设理解为差值吧

select 整型字段1,整型字段2,整型字段1-整型字段2 as 差值 from tablename
zjcxc 2003-09-26
  • 打赏
  • 举报
回复
什么间距值?
sdhdy 2003-09-26
  • 打赏
  • 举报
回复
?不太明白!
zjcxc 2003-09-26
  • 打赏
  • 举报
回复
如果是得到字符串,就只需要将显示结果部分改成:

--得到结果:
declare @str varchar(8000)
set @str=''
select @str=@str+','+cast(a.bid as varchar)
+case a.bid when b.eid then '' else '--'+cast(b.eid as varchar) end
from #tb1 a,#tb2 b
where a.id=b.id

set @str=substring(@str,2,8000)
print '结果:'+@str
mjhnet 2003-09-26
  • 打赏
  • 举报
回复
select isnull(a-b,0) as id_num from
(select 字段 a, ranka = (select count(*) from 表 AA where AA.字段<A.字段)
from 表 A) C
left outer join
(select 字段 b, rankb = (select count(*) from 表 BB where BB.字段<B.字段)
from 表 B) D
on C.ranka = D.rankb+1
zjcxc 2003-09-26
  • 打赏
  • 举报
回复
按表结果显示:

--创建数据测试环境
declare @tb table(ID int)
insert into @tb
select 25003
union all select 25004
union all select 25007
union all select 25010
union all select 25015

--得到每个连号的开始编号
select id=identity(int,1,1),cast(id as int) as bid
into #tb1 from @tb a
where not exists(select 1 from @tb where id=a.id-1)

--得到每个连号的结束编号
select id=identity(int,1,1),cast(id as int) as eid
into #tb2 from @tb a
where not exists(select 1 from @tb where id=a.id+1)

--显示结果
select a.bid as 连号开始编号,b.eid as 连号结束编号
from #tb1 a,#tb2 b
where a.id=b.id

--删除临时表
drop table #tb1,#tb2
zjcxc 2003-09-26
  • 打赏
  • 举报
回复
你是想得到编号的字符串?还是表?
zjcxc 2003-09-26
  • 打赏
  • 举报
回复
如果能保证你的相邻两行的数据不重复的话,可以用下面的方法:


select 差额=isnull(id-(select max(id) from 表 where id<a.id),0)
from 表 a
XiaoCsharp 2003-09-26
  • 打赏
  • 举报
回复
是同一个字段的前后值
XiaoCsharp 2003-09-26
  • 打赏
  • 举报
回复
我的目的是要得到一个产品的编号,如果连续,就只显示起止号,如果不连接,就列出号码.
例如:上面的应该得到
25003-25004,25007,25010,25015....
XiaoCsharp 2003-09-26
  • 打赏
  • 举报
回复
对不起,我没有说清楚,其实是求一个字段的前后间的间隔值:
例如:有一个字段
ID
25003
25004
25007
25010
25015
如何得到
ID_Num
0
1
3
3
5
之类的值,
谢谢各位



22,209

社区成员

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

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