【测试数据】
CREATE TABLE temp (
year INT,
salary INT
);
INSERT INTO temp VALUES(2000, 1000);
INSERT INTO temp VALUES(2001, 2000);
INSERT INTO temp VALUES(2002, 3000);
INSERT INTO temp VALUES(2003, 4000);
【视图codes:】
SELECT
year,
salary - ISNULL((SELECT TOP 1 salary FROM temp t2 WHERE t2.year < temp.year ORDER BY year DESC), 0) AS salary
FROM
temp;
go
测试结果