SQL数据库中求上下相邻两行的差
如图所示,求高手指点!
------解决方案--------------------2005以上可以用row_number()over来给每行加上一个自增标识符,然后再处理
------解决方案--------------------select *,abs((id-(select id from table1 where id>t.id order by id limit 1)))
as result
from table1 t
------解决方案--------------------select *,(select id from table1 where id>t.id order by id limit 1)-id
as result
from table1 t
------解决方案--------------------with tb as
(
select *, rownr = ROW_NUMBER()VOER(ORDER BY id)
from table1
)
update table1
set result = y.id-x.id
from tb x
left outer join tb y on x.rownr +1 = y.rownr