--> 测试数据: @A表 declare @A表 table (id int,col int) insert into @A表 select 1,5 union all select 2,20 union all select 3,30--> 测试数据: @B表 declare @B表 table (id int,col int) insert into @B表 select 1,0 union all select 2,5 union all select 3,25select a.col AS A,a.col+b.col AS B from @A表 a LEFT JOIN @B表 b ON a.id=b.id/* A B ----------- ----------- 5 5 20 25 30 55 */
declare @A表 table (id int,col int) insert into @A表 select 1,5 union all select 2,20 union all select 3,30SELECT a.col AS A , (SELECT SUM(col) FROM @A表 WHERE id <= a.id) AS B FROM @A表 a /* A B ----------- ----------- 5 5 20 25 30 55 */ 只有一个表,这样就可以了
5 5
20 25
30 55
declare @A表 table (id int,col int)
insert into @A表
select 1,5 union all
select 2,20 union all
select 3,30--> 测试数据: @B表
declare @B表 table (id int,col int)
insert into @B表
select 1,0 union all
select 2,5 union all
select 3,25select a.col AS A,a.col+b.col AS B from @A表 a
LEFT JOIN @B表 b ON a.id=b.id/*
A B
----------- -----------
5 5
20 25
30 55
*/
declare @A表 table (id int,col int)
insert into @A表
select 1,5 union all
select 2,20 union all
select 3,30SELECT a.col AS A ,
(SELECT SUM(col) FROM @A表 WHERE id <= a.id) AS B
FROM @A表 a
/*
A B
----------- -----------
5 5
20 25
30 55
*/
只有一个表,这样就可以了