表名是 t1内如下,这个余额是在表temp1 内根据客户编号来取得,请问如何得t1内的余 额累加值?根据借方就加,贷就减,但是如何根据上面得到的值累加呢?
客户编号 借方 货方 余额
00001 1000
00001 100 0 1100
00001 0 200 900
00001 100 200 900
00002 0 0 6000
00002 200 0 6200
00002 0 100 6100
temp1 如下:
客户编号 余额
00001 1000
00002 6000
客户编号 借方 货方 余额
00001 1000
00001 100 0 1100
00001 0 200 900
00001 100 200 900
00002 0 0 6000
00002 200 0 6200
00002 0 100 6100
temp1 如下:
客户编号 余额
00001 1000
00002 6000
00001 1000
00001 100 0 1100 --这是1000(从temp1内取得的)+100(借方)
00001 0 200 900 --这是1100-200(贷方)
00001 100 200 900 ---说明一下,这是上面00001的合计
00002 0 0 6000
00002 200 0 6200
00002 0 100 6100
00002 200 100 6100 ---说明一下,这是上面00002的合计
temp1 如下:
客户编号 余额
00001 1000
00002 6000
客户编号 余额
00001 1000
00002 6000 这个怎么来的,为什么不是900和6100?
if object_id('[t1]') is not null drop table [t1]
go
create table [t1]([客户编号] varchar(5),[借方] int,[货方] int,[余额] int)
insert [t1]
select '00001',null,null,0 union all
select '00001',100,0,0 union all
select '00001',0,200,0 union all
select '00001',100,200,0 union all
select '00002',0,0,0 union all
select '00002',200,0,0 union all
select '00002',0,100,0
if object_id('[temp1]') is not null drop table [temp1]
go
create table [temp1]([客户编号] varchar(5),[余额] int)
insert [temp1]
select '00001',1000 union all
select '00002',6000
---查询---select tid=identity(int,1,1),* into # from t1select t.[客户编号],t.[借方],t.[货方],
[余额]=(select isnull(te.[余额],0)+sum(isnull([借方],0))-sum(isnull([货方],0)) from # where [客户编号]=t.[客户编号] and tid<=t.tid)
from # t
left join temp1 te on te.[客户编号]=t.[客户编号]drop table #---结果---
客户编号 借方 货方 余额
----- ----------- ----------- -----------
00001 NULL NULL 1000
00001 100 0 1100
00001 0 200 900
00001 100 200 800
00002 0 0 6000
00002 200 0 6200
00002 0 100 6100(所影响的行数为 7 行)
00001 NULL NULL 1000 这个初始余额呢?
---测试数据---
if object_id('[t1]') is not null drop table [t1]
go
create table [t1]([客户编号] varchar(5),[借方] int,[货方] int,[余额] int)
insert [t1]
select '00001',100,0,0 union all
select '00001',0,200,0 union all
select '00001',100,200,0 union all
select '00002',200,0,0 union all
select '00002',0,100,0
if object_id('[temp1]') is not null drop table [temp1]
go
create table [temp1]([客户编号] varchar(5),[余额] int)
insert [temp1]
select '00001',1000 union all
select '00002',6000
---查询---select tid=identity(int,1,1),* into # from (select * from t1 union select [客户编号],null [借方],null [货方],[余额] from temp1) taselect t.[客户编号],t.[借方],t.[货方],
[余额]=(select isnull(te.[余额],0)+sum(isnull([借方],0))-sum(isnull([货方],0)) from # where [客户编号]=t.[客户编号] and tid<=t.tid)
from # t
left join temp1 te on t.[客户编号]=te.[客户编号]drop table #/**
客户编号 借方 货方 余额
----- ----------- ----------- -----------
00001 NULL NULL 1000
00001 0 200 800
00001 100 0 900
00001 100 200 800
00002 NULL NULL 6000
00002 0 100 5900
00002 200 0 6100(所影响的行数为 7 行)
**/
----- ----------- ----------- -----------
00001 NULL NULL 1000
00001 0 200 800
00001 100 0 900
00001 100 200 800 ---这一行错了!!!是900
00002 NULL NULL 6000
00002 0 100 5900
00002 200 0 6100 ---这个下面没有汇总
----- ----------- ----------- -----------
00001 NULL NULL 1000
00001 0 200 800
00001 100 0 900
00001 100 200 800 ---应该是这行下面还要出一行汇总
00001 200 400 800 ---就是少了这一行(借方200是上面借少的总和,400是上面贷方的总和,800是余额00002 NULL NULL 6000
00002 0 100 5900
00002 200 0 6100 ---这个下面没有汇总 00002 200 100 6100 ----还有少了这一行