不要用游标update a set
   net = case when b.price is null then 0 else a.price-b.price end
from tab a left join tab b
on b.time = (select max(time) from tab where time<a.time)
where a.net is null

解决方案 »

  1.   

    有很多记录, 并且每隔1分钟就有一条新的数据添加进来
    现在要求用sql语句实现:net的值是这一行的price减去上一行price的差值,当然第一行的net值是0,也就是说id为2的net值-0.5,id为3的net值-1.74······。
    要求数据库每隔1分钟就检查一次,如果表中net值存在null值,就计算,已经计算过的就不用计算了,
    -------------------为什么不在数据添加的时候做?服务器太空闲了是吧,再开个作业或线程去检查这些无谓的东西。