MySQL 与先前值的增量
MySQL delta from previous value
我有一个像这样的table:
而且我需要添加一个列,其中包含基于数据顺序的先前值的增量值,如下所示:
我怎样才能完成这个任务?
table 上的一个简单示例,它只有从 0 到 9 的整数值:
select
i,
lead(i) over (order by i) as "LEAD",
lag(i) over (order by i) as "LAG"
from numm
order by i;
将输出:
+---+------+------+
| i | LEAD | LAG |
+---+------+------+
| 0 | 1 | NULL |
| 1 | 2 | 0 |
| 2 | 3 | 1 |
| 3 | 4 | 2 |
| 4 | 5 | 3 |
| 5 | 6 | 4 |
| 6 | 7 | 5 |
| 7 | 8 | 6 |
| 8 | 9 | 7 |
| 9 | NULL | 8 |
+---+------+------+
我有一个像这样的table:
而且我需要添加一个列,其中包含基于数据顺序的先前值的增量值,如下所示:
我怎样才能完成这个任务?
table 上的一个简单示例,它只有从 0 到 9 的整数值:
select
i,
lead(i) over (order by i) as "LEAD",
lag(i) over (order by i) as "LAG"
from numm
order by i;
将输出:
+---+------+------+
| i | LEAD | LAG |
+---+------+------+
| 0 | 1 | NULL |
| 1 | 2 | 0 |
| 2 | 3 | 1 |
| 3 | 4 | 2 |
| 4 | 5 | 3 |
| 5 | 6 | 4 |
| 6 | 7 | 5 |
| 7 | 8 | 6 |
| 8 | 9 | 7 |
| 9 | NULL | 8 |
+---+------+------+