sql 实现某列中,后一个数字减去前一个数字,取得差值,写入新表

2024-12-12 19:56:40
推荐回答(3个)
回答1:

主键是什么啊?如果主键是ID + number列的话:
declare @ID INT, @number INT
create table #T_ID( ID INT )
create table #TI ( number int, cellid int )
inset #T_ID select distinct ID from 原表 order by 1
while ( select count(*) from #T_ID ) > 0
select top 1 @ID = ID from #T_ID
delete #T_ID WHERE @ID = ID
delete #T1
inset #T1 select number, cellID from 原表 where ID = @ID
while ( select count(*) from #T1 ) > 0
begin
select top 1 @number = number from #T1
delete #T1 WHERE @number = number
insert 新表
select @ID, @number, cellID, @number - isnull( ( select top 1 number from 原表 where ID = @ID and number < @number order by number ), @number )
FROM 原表 where ID = @ID and @number = number
end
end

回答2:

你这不是写出来了么?
numberinterval=select * from nub2-nub1
哦,原来ID是个线程,哈哈那这个可复杂了!
你给的分少,我可不答!

回答3:

纯SQL完成我也不会,关注一下。
郁闷的是,没有一个标识列。
貌似还要按ID分组计算