版主,高手麻烦您们来看一眼

huangjinyin 2006-11-10 04:27:40
有一表如下
科目 方向 借方 贷方 余额
1001 0 0 0 3500(是现成数据)第一行
1001 0 500 0 0
1001 0 300 0 0
1001 0 0 1000 0
2101 1 0 0 1000(是现成数据)第一行
2101 1 0 300 0
2101 1 0 400 0
2101 1 600 0 0
2101 1 50 0 0
.....
现在想得到如下结果:
科目 方向 借方 贷方 余额
1001 0 0 0 3500(是现成数据)第一行
1001 0 500 0 4000(4000要通过计算的)第二行
1001 0 300 0 4300
1001 0 0 1000 3300
2101 1 0 0 1000(是现成数据)第一行
2101 1 0 300 1300
2101 1 0 400 1700
2101 1 600 0 1100
2101 1 50 0 950
.......
这里边的余额就第一条是现成数据
下面的都是要经过计算得出来的,公式如下

if(方向==0) 某一行的余额=上一行余额+第二行借方-第二行货方
if(方向==1) 某一行的余额=上一行余额-第二行借方+第二行货方
不要用游标,游标太慢
...全文
216 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
稍微简化下 libin_ftsafe 的 查询语句
create table test(科目 varchar(8),方向 int,借方 int,贷方 int,余额 int)
insert into test select '1001',0,0 ,0 ,3500
insert into test select '1001',0,500,0 ,0
insert into test select '1001',0,300,0 ,0
insert into test select '1001',0,0 ,1000,0
insert into test select '2101',1,0 ,0 ,1000
insert into test select '2101',1,0 ,300 ,0
insert into test select '2101',1,0 ,400 ,0
insert into test select '2101',1,600,0 ,0
insert into test select '2101',1,50 ,0 ,0
go


alter table test add id int identity(1,1)
go

update t
set
余额=(select sum(余额+(1-方向*2)*(借方-贷方)) from test where 科目=t.科目 and id<=t.id)
from
test t
go

alter table test drop column id
go

select * from test
go

drop table test
hhhdyj 2006-11-10
  • 打赏
  • 举报
回复
搂主不好意思,忘考虑方向了,修改一下
UPDATE @tb
SET @sum = CASE WHEN @科目 = 科目 AND 方向 = 0 THEN @sum + ISNULL(借方, 0) - ISNULL(贷方, 0)
WHEN @科目 = 科目 AND 方向 = 1 THEN @sum - ISNULL(借方, 0) + ISNULL(贷方, 0)
ELSE 余额 END,
余额 = @sum,
@科目 = 科目
hhhdyj 2006-11-10
  • 打赏
  • 举报
回复
DECLARE @tb TABLE([科目] int, [方向] int, [借方] int, [贷方] int, [余额] int)
INSERT INTO @tb
SELECT 1001, 0, 0, 0, 3500
UNION ALL SELECT 1001, 0, 500, 0, 0
UNION ALL SELECT 1001, 0, 300, 0, 0
UNION ALL SELECT 1001, 0, 0, 1000, 0
UNION ALL SELECT 2101, 1, 0, 0, 1000
UNION ALL SELECT 2101, 1, 0, 300, 0
UNION ALL SELECT 2101, 1, 0, 400, 0
UNION ALL SELECT 2101, 1, 600, 0, 0
UNION ALL SELECT 2101, 1, 50, 0, 0

DECLARE @sum INT
DECLARE @科目 INT
SELECT @sum = 0, @科目 = 0
UPDATE @tb
SET @sum = CASE WHEN @科目 = 科目 THEN @sum + ISNULL(借方, 0) - ISNULL(贷方, 0) ELSE 余额 END,
余额 = @sum,
@科目 = 科目
SELECT * FROM @tb

科目 方向 借方 贷方 余额
----------- ----------- ----------- ----------- -----------
1001 0 0 0 3500
1001 0 500 0 4000
1001 0 300 0 4300
1001 0 0 1000 3300
2101 1 0 0 1000
2101 1 0 300 700
2101 1 0 400 300
2101 1 600 0 900
2101 1 50 0 950
huangjinyin 2006-11-10
  • 打赏
  • 举报
回复
我按照libin_ftsafe(子陌红尘:当libin告别ftsafe)的方法试了,可是会有语法错误,
不知libin_ftsafe兄可否加我QQ44921619,非常感谢大家的帮忙,努力解决中
dawugui 2006-11-10
  • 打赏
  • 举报
回复
参考这个:

例:有一个收支表(szb)如下结构

日期(RQ),...项目(XM),收入, 支出
2006.10.01 收工资 500
2006.10.02 付房租 200
2006.10.03 购物 50

目标说明: 要用一条SQL语句生成结果集如下(比上表多一个结余)
每条记录的 结 = 上一条记录的结余+当前记录收入-当前记录支出

日期(RQ),...项目(XM),收入, 支出
2006.10.01 收工资 500
2006.10.02 付房租 200
2006.10.03 购物 50

declare @t table (
收支日期RQ datetime,
收支项目XM varchar(20),
收入金额SL int,
支出金额ZC int
)
insert @t
select '2006.10.01','收工资',500,0 union all
select '2006.10.02','付房租',0,200 union all
select '2006.10.03','购物支出',0,50


select 收支日期RQ , 收支项目XM , 收入金额SL , 支出金额ZC , 结余JY = isnull(收入金额SL - 支出金额ZC ,0) into #temp from @t

update #temp
set 结余JY=(select isnull(sum(收入金额SL - 支出金额ZC),0)
from #temp tt
where TT.收支日期RQ<=#temp.收支日期RQ)
select convert(varchar(10),收支日期RQ,120) as 收支日期RQ , 收支项目XM , 收入金额SL , 支出金额ZC , 结余JY from #temp

drop table #temp


--结果
收支日期RQ 收支项目XM 收入金额SL 支出金额ZC 结余JY
---------- ---------- ---------- ---------- -----------
2006-10-01 收工资 500 0 500
2006-10-02 付房租 0 200 300
2006-10-03 购物支出 0 50 250

(所影响的行数为 3 行)

zsforever 2006-11-10
  • 打赏
  • 举报
回复
up
子陌红尘 2006-11-10
  • 打赏
  • 举报
回复
create table test(科目 varchar(8),方向 int,借方 int,贷方 int,余额 int)
insert into test select '1001',0,0 ,0 ,3500
insert into test select '1001',0,500,0 ,0
insert into test select '1001',0,300,0 ,0
insert into test select '1001',0,0 ,1000,0
insert into test select '2101',1,0 ,0 ,1000
insert into test select '2101',1,0 ,300 ,0
insert into test select '2101',1,0 ,400 ,0
insert into test select '2101',1,600,0 ,0
insert into test select '2101',1,50 ,0 ,0
go


alter table test add id int identity(1,1)
go

update t
set
余额=(select sum(余额+(case 方向 when 0 then 1 else -1 end)*(借方-贷方)) from test where 科目=t.科目 and id<=t.id)
from
test t
go

alter table test drop column id
go

select * from test
go

/*
科目 方向 借方 贷方 余额
-------- ----------- ----------- ----------- -----------
1001 0 0 0 3500
1001 0 500 0 4000
1001 0 300 0 4300
1001 0 0 1000 3300
2101 1 0 0 1000
2101 1 0 300 1300
2101 1 0 400 1700
2101 1 600 0 1100
2101 1 50 0 1050
*/

drop table test
go

27,581

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧