declare @qc int select @qc=1000 select identity(int,1,1) id,* into #tmp from 表1 select 客户名称, 应收增加, 应收减少,(select @qc+sum(应收增加)-sum(应收减少) from #tmp b where a.id=b.id) 应收余额 from #tmp a drop #tmp
原表结构 不清,第一直觉,里面应有还有一个字段,比方说有 datetime 最好!
txlicenhe(马可) :说的已经很清楚了,可以实现的
--在你的原表上增加一个字段:应收余额 --然后用下面的方法处理declare @i int,@j int set @i=1000 --期初余额update 表 set @j=@i+应收增加-应收减少 ,@i=@j,应收余额=@j
--下面是例子:--测试数据 declare @t table(客户名称 varchar(10),应收增加 int,应收减少 int,应收余额 int) insert into @t(客户名称,应收增加,应收减少) select '张三',100,0 union all select '张三',100,0 union all select '张三',0,100 union all select '张三',100,0 union all select '张三',100,0--显示处理前的表内容 select * from @t--处理 declare @i int,@j int set @i=1000 --期初余额update @t set @j=@i+应收增加-应收减少 ,应收余额=@i,@i=@j--显示处理后的结果 select * from @t/*--测试结果 --处理前的结果客户名称 应收增加 应收减少 应收余额 ---------- ----------- ----------- ----------- 张三 100 0 NULL 张三 100 0 NULL 张三 0 100 NULL 张三 100 0 NULL 张三 100 0 NULL(所影响的行数为 5 行) --处理后结果 客户名称 应收增加 应收减少 应收余额 ---------- ----------- ----------- ----------- 张三 100 0 1100 张三 100 0 1200 张三 0 100 1100 张三 100 0 1200 张三 100 0 1300(所影响的行数为 5 行) --*/
--做成视图,你的表中增加一个标识字段才行.--下面是例子:--测试数据 declare @t table(id int identity(1,1),客户名称 varchar(10),应收增加 int,应收减少 int,应收余额 int) insert into @t(客户名称,应收增加,应收减少) select '张三',100,0 union all select '张三',100,0 union all select '张三',0,100 union all select '张三',100,0 union all select '张三',100,0--查询 select *,应收余额=1000+(select sum(应收增加-应收减少) from @t where id<=a.id) from @t a/*--查询结果 id 客户名称 应收增加 应收减少 应收余额 应收余额 ----------- ---------- ----------- ----------- ----------- ----------- 1 张三 100 0 NULL 1100 2 张三 100 0 NULL 1200 3 张三 0 100 NULL 1100 4 张三 100 0 NULL 1200 5 张三 100 0 NULL 1300(所影响的行数为 5 行) --*/
--即,设id为标识字段:create view 视图名 as select *,应收余额=1000+(select sum(应收增加-应收减少) from 表 where id<=a.id) from 表 a
必须增加一个ID字段才能唯一索引 SELECT name, aadd, redu ,isnull((select sum(aadd-redu) from table1 k where k.id < k1.id ),0)+aadd-redu+1000 as total FROM dbo.TABLE1 k1
select @qc=1000
select identity(int,1,1) id,* into #tmp from 表1
select 客户名称, 应收增加, 应收减少,(select @qc+sum(应收增加)-sum(应收减少) from #tmp b where a.id=b.id) 应收余额 from #tmp a
drop #tmp
--然后用下面的方法处理declare @i int,@j int
set @i=1000 --期初余额update 表 set @j=@i+应收增加-应收减少
,@i=@j,应收余额=@j
declare @t table(客户名称 varchar(10),应收增加 int,应收减少 int,应收余额 int)
insert into @t(客户名称,应收增加,应收减少)
select '张三',100,0
union all select '张三',100,0
union all select '张三',0,100
union all select '张三',100,0
union all select '张三',100,0--显示处理前的表内容
select * from @t--处理
declare @i int,@j int
set @i=1000 --期初余额update @t set @j=@i+应收增加-应收减少
,应收余额=@i,@i=@j--显示处理后的结果
select * from @t/*--测试结果
--处理前的结果客户名称 应收增加 应收减少 应收余额
---------- ----------- ----------- -----------
张三 100 0 NULL
张三 100 0 NULL
张三 0 100 NULL
张三 100 0 NULL
张三 100 0 NULL(所影响的行数为 5 行)
--处理后结果
客户名称 应收增加 应收减少 应收余额
---------- ----------- ----------- -----------
张三 100 0 1100
张三 100 0 1200
张三 0 100 1100
张三 100 0 1200
张三 100 0 1300(所影响的行数为 5 行)
--*/
declare @t table(id int identity(1,1),客户名称 varchar(10),应收增加 int,应收减少 int,应收余额 int)
insert into @t(客户名称,应收增加,应收减少)
select '张三',100,0
union all select '张三',100,0
union all select '张三',0,100
union all select '张三',100,0
union all select '张三',100,0--查询
select *,应收余额=1000+(select sum(应收增加-应收减少) from @t where id<=a.id)
from @t a/*--查询结果
id 客户名称 应收增加 应收减少 应收余额 应收余额
----------- ---------- ----------- ----------- ----------- -----------
1 张三 100 0 NULL 1100
2 张三 100 0 NULL 1200
3 张三 0 100 NULL 1100
4 张三 100 0 NULL 1200
5 张三 100 0 NULL 1300(所影响的行数为 5 行)
--*/
as
select *,应收余额=1000+(select sum(应收增加-应收减少) from 表 where id<=a.id)
from 表 a
SELECT name, aadd, redu ,isnull((select sum(aadd-redu) from table1 k where k.id < k1.id ),0)+aadd-redu+1000 as total
FROM dbo.TABLE1 k1