Mysql
 sql >> Database >  >> RDS >> Mysql

differenza tra due righe consecutive in mysql

Usa un self-join:

SELECT
    t1.ts,
    t1.number,
    t1.number - COALESCE(t2.number, t1.number) AS diff
FROM yourTable t1
LEFT JOIN yourTable t2
    ON t1.id = t2.id + 1
ORDER BY
    t1.ts;

Dimostrazione