答案 0 :(得分:0)
您可以进行如下所示的自我加入,并在必要时提供条件
SELECT CurrYear.MONTH,CurrYear.Yr,CurrYear.VAL-PrevYear.val as diff
FROM #Table1 CurrYear INNER JOIN #Table1 PrevYear ON CurrYear.Yr = PrevYear.Yr-1 AND CurrYear.MONTH = PrevYear.MONTH
答案 1 :(得分:-1)
您没有说明您的DBMS,但使用标准SQL通常使用窗口函数(所有现代DBMS都支持):
select month, year, sales,
sales - lag(sales) over (order by year, month) as difference
from the_table
order by year, month;