https://blog.csdn.net/sinat_26811377/article/details/107188400 参考
SELECT
a.TYEAR,
a.TMONTH,
a.TAMOUNT,
a.TAMOUNT - COALESCE(b.TAMOUNT, 0) AS DELTA
FROM
test_mount a
LEFT JOIN test_mount b ON
IF(
a.TMONTH = 1,
a.TYEAR -1,
a.TYEAR
) = b.TYEAR AND IF(a.TMONTH = 1, 12, a.TMONTH - 1) = b.TMONTH
sql 话 有个 lag 开窗函数,可以了解下
select *,lag(TAMOUNT,1) OVER(ORDER BY TYEAR,TMONTH) TAMOUNT1 FROM sql2001 order by TYEAR,TMONTH