[英]Difference between rows in MySQL
I would like to generate a new table in mysql which contains the difference between every two rows.. For example, if I have this table: 我想在mysql中生成一个新表,其中包含每两行之间的差异。例如,如果我有此表:
table1: 表格1:
ID VALUE
1 16748
1 19628
1 23245
1 23322
1 33399
2 15367
2 17839
2 34578
2 53421
3 14229
3 19533
........
I would like to get the difference between 2 consecutive values (row[x+1]-row[x]), for all values per ID 我想获得2个连续值(row [x + 1] -row [x])之间的差,针对每个ID
So my result would be: 所以我的结果是:
ID DIFF
1 2880
1 3617
1 77
1 10077
2 2472
2 16739
2 18843
3 5304
You can try something like this:- 您可以尝试这样的事情:
SELECT
MIN(T2.VALUE- T1.VALUE) AS Dif
FROM
Table T1 INNER JOIN Table T2 on T1.VALUE< T2.VALUE
ORDER BY
T1.VALUE
您可以通过将表与其自身连接来完成此操作。
SELECT (first.value - second.value) AS differ FROM `table` AS first JOIN `table` AS second WHERE first.id + 1 = second.id
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.