22,294
社区成员
发帖
与我相关
我的任务
分享--> 测试数据: #tb
if object_id('tempdb.dbo.#tb') is not null drop table #tb
go
create table #tb (SZMX varchar(1),SRJE int,ZCJE int,XLH int)
insert into #tb
select 'A',100,0,1 union all
select 'B',200,0,2 union all
select 'C',0,300,3 union all
select 'D',0,50,4 union all
select 'H',100,30,5 union all
select 'F',50,100,6
if object_id('tempdb.dbo.#ta') is not null drop table #ta
go
create table #ta (SZMX varchar(1),SRJE int,ZCJE int,jcJE int,XLH int)
insert into #ta
select 'w',170,80,1090,8
insert into #ta
select SZMX,SRJE,ZCJE, jcje=(select top 1 jcJE from #ta order by xlh desc)
+isnull((select sum(SRJE)-sum(ZCJE) from #tb where xlh<=t.xlh),0)
,XLH
from #tb t
select * from #ta
SZMX SRJE ZCJE jcJE XLH
---- ----------- ----------- ----------- -----------
w 170 80 1090 8
A 100 0 1190 1
B 200 0 1390 2
C 0 300 1090 3
D 0 50 1040 4
H 100 30 1110 5
F 50 100 1060 6
(7 行受影响)--> 测试数据: #tb
if object_id('tempdb.dbo.#tb') is not null drop table #tb
go
create table #tb (SZMX varchar(1),SRJE int,ZCJE int,XLH int)
insert into #tb
select 'A',100,0,1 union all
select 'B',200,0,2 union all
select 'C',0,300,3 union all
select 'D',0,50,4 union all
select 'H',100,30,5 union all
select 'F',50,100,6
select *,jcje=(select sum(SRJE)-sum(ZCJE) from #tb where XLH<=t.XLH)
from #tb t
SZMX SRJE ZCJE XLH jcje
---- ----------- ----------- ----------- -----------
A 100 0 1 100
B 200 0 2 300
C 0 300 3 0
D 0 50 4 -50
H 100 30 5 20
F 50 100 6 -30
(6 行受影响)
--你用代码格式把数据调整一下,看的头晕...