表ck
id ye1 ye2
1 2 3
2 3 4
3 4 5
想得到
id ye1 ye2 ye
1 7 2 5(7-2)
2 3 4 4(5+3-4)
3 4 5 3(4+4-5)如何得到这个ye
id ye1 ye2
1 2 3
2 3 4
3 4 5
想得到
id ye1 ye2 ye
1 7 2 5(7-2)
2 3 4 4(5+3-4)
3 4 5 3(4+4-5)如何得到这个ye
调试欢乐多
if object_id('[ck]') is not null drop table [ck]
go
create table [ck]([id] int,[ye1] int,[ye2] int)
insert [ck]
select 1,7,2 union all
select 2,3,4 union all
select 3,4,5
---查询---
select
*,
ye=(select sum(ye1)-sum(ye2) from ck where id<=t.id)
from ck t---结果---
id ye1 ye2 ye
----------- ----------- ----------- -----------
1 7 2 5
2 3 4 4
3 4 5 3(所影响的行数为 3 行)
id ye1 ye2
1 2 3
2 3 4
3 4 5
这样的好理解一些吧
if object_id('[ck]') is not null drop table [ck]
go
create table [ck]([id] int,[ye1] int,[ye2] int)
insert [ck]
select 1,7,2 union all
select 2,3,4 union all
select 3,4,5
---查询---
select
*,
ye=(select sum(ye1)-sum(ye2) from ck where id<=t.id)
from ck t---结果---
id ye1 ye2 ye
----------- ----------- ----------- -----------
1 7 2 5
2 3 4 4
3 4 5 3(所影响的行数为 3 行)