|
[code=sql]USE tempdb
GO
IF OBJECT_ID('TEST') IS NOT NULL
DROP TABLE TEST;
GO
CREATE TABLE TEST
(
id int identity(1,1),
num int
);
GO
INSERT INTO TEST VALUES
(848),
(269),
(603),
(087),
(166);
GO
----------------------------------------
select a.*,a.num+b.num as [1],a.num+b.num+c.num as [2]
from TEST as a
left join TEST as b
on a.id - b.id =1
left join TEST as c
on a.id - c.id =2
----------------------------------------
/*
id num 1 2
----------- ----------- ----------- -----------
1 848 NULL NULL
2 269 1117 NULL
3 603 872 1720
4 87 690 959
5 166 253 856
(5 行受影响)
*/[/code] |
|