计算公里数运行基于此current value = next value - current value
我有一张看起来像这样的桌子。
我的问题是如何根据里程表的值计算kmr
?我将替换kmr值的kmr列的值
最佳答案
这将使用等级,并且超过一个人
http://sqlfiddle.com/#!9/4c054e/1
Select m.`code`,m.vdate, ( n.kmr - m.kmr) as new_kmr
From
(Select t1.*, @rnk := @rnk + 1 as rnk
From tbl t1, (select @rnk := 0) t
Order by t1.`code`,t1.vdate) m left join
(Select t2.*, @rnk1 := @rnk1 + 1 as rnk
From tbl t2, (select @rnk1:= 0) t
Order by t2.`code`,t2.vdate) n
On m.`code` = n.`code`
And m.rnk + 1 = n.rnk
Order by m.`code`, m.vdate
输出:
code vdate new_kmr
person 1 2018-03-01 10
person 1 2018-03-02 10
person 1 2018-03-03 (null)
person 2 2018-03-01 5
person 2 2018-03-02 (null)
关于mysql - 减去当前值,然后将该输出作为当前行的数据,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49444985/