if exists (select * from sysobjects where id = OBJECT_ID('[test]') and OBJECTPROPERTY(id, 'IsUserTable') = 1)
DROP TABLE [test]

CREATE TABLE [test] (
[id] [nchar]  (10) NULL,
[A] [int]  NULL,
[B] [int]  NULL)

INSERT [test] ([id],[A],[B]) VALUES ( '1',1,2)
INSERT [test] ([id],[A],[B]) VALUES ( '2',3,4)
INSERT [test] ([id],[A],[B]) VALUES ( '3',5,6)
INSERT [test] ([id],[A],[B]) VALUES ( '4',7,8)
INSERT [test] ([id],[A],[B]) VALUES ( '5',9,10)
INSERT [test] ([id],[A],[B]) VALUES ( '6',11,12)

 

SELECT  a.id ,A,B ,
(SELECT SUM(A+B)FROM  test b where b.id<=a.id) as total
FROM test  a

 

 更多参考 http://www.cio360.net/h/2253/348611-9313.html