这个问题是基于下面我的上一个问题。 Get percentage based on value in previous row
与其获取一条select语句, 我希望更新语句使用更新查询来填充朗姆酒列。
输入:
+---+--------+------+
|CID| number | rum |
+---+--------+------+
| 1 | 1.0000 | NULL |
| 3 | 2.0000 | NULL |
| 5 | 2.0000 | NULL |
| 6 | 4.0000 | NULL |
+---+--------+------+
输出:
+---+--------+------+
|CID| number | rum |
+---+--------+------+
| 1 | 1.0000 | NULL |
| 3 | 2.0000 |100.0 |
| 5 | 2.0000 | 0.0 |
| 6 | 4.0000 |100.0 |
+---+--------+------+
公式:
rum = (currNumber - prevNumber) / prevNumber * 100
答案 0 :(得分:0)
您可以在JOIN
中使用UPDATE
和窗口函数。一种方法是:
update t join
(select t.*, lag(number) over (order by cid) as prev_number
from t
) tt
on tt.cid = t.cid
set rum = (number - prev_number) * 100 / prev_number;