22,294
社区成员
发帖
与我相关
我的任务
分享create table tb(序号 int,提货金额 int,期末金额 int,类型 varchar(10))
insert into tb values(1 ,10 ,0,'提货')
insert into tb values(2 ,20 ,0,'提货')
insert into tb values(3 ,30 ,0,'提货')
insert into tb values(4 ,10 ,0,'提货')
insert into tb values(5 ,20 ,0,'回款')
insert into tb values(6 ,10 ,0,'提货')
go
declare @je as int
set @je = 1000
select 序号 ,提货金额 ,类型, 期末金额 = @je - (select sum(case when 类型 = '提货' then 提货金额 else -提货金额 end) from tb where 序号 <= t.序号) from tb t
drop table tb
/*
序号 提货金额 类型 期末金额
----------- ----------- ---------- -----------
1 10 提货 990
2 20 提货 970
3 30 提货 940
4 10 提货 930
5 20 回款 950
6 10 提货 940
(所影响的行数为 6 行)
*/create table tb(序号 int,提货金额 int,期末金额 int,类型 varchar(10))
insert into tb values(1 ,10 ,0,'提货')
insert into tb values(2 ,20 ,0,'提货')
insert into tb values(3 ,30 ,0,'提货')
insert into tb values(4 ,10 ,0,'提货')
insert into tb values(5 ,20 ,0,'回款')
insert into tb values(6 ,10 ,0,'提货')
go
declare @cq as int
set @cq = 1000
select t.序号 ,t.提货金额 , 期末金额 =1000+SUM(case when k.类型='提货' then -k.提货金额 else k.提货金额 end)
from tb t join tb k on k.序号 <= t.序号
group by t.序号 ,t.提货金额
order by t.序号
drop table tb
/*
序号 提货金额 期末金额
----------- ----------- -----------
1 10 990
2 20 970
3 30 940
4 10 930
5 20 950
6 10 940*/declare @cq as int
set @cq = 1000
select t.序号 ,t.提货金额 , 期末金额 =1000-SUM(k.提货金额)
from tb t join tb k on k.序号 <= t.序号
group by t.序号 ,t.提货金额
order by t.序号
/*
序号 提货金额 期末金额
----------- ----------- -----------
1 10 990
2 20 970
3 30 940
4 10 930*/create table tb(序号 int,提货金额 int,期末金额 int)
insert into tb values(1 ,10 ,0)
insert into tb values(2 ,20 ,0)
insert into tb values(3 ,30 ,0)
insert into tb values(4 ,10 ,0)
go
declare @je as int
set @je = 1000
select 序号 ,提货金额 , 期末金额 = @je - (select sum(提货金额) from tb where 序号 <= t.序号) from tb t
drop table tb
/*
序号 提货金额 期末金额
----------- ----------- -----------
1 10 990
2 20 970
3 30 940
4 10 930
(所影响的行数为 4 行)
*/