请教这个语句应该怎么写?达人请进~~~

冷箫轻笛 2004-05-10 11:36:56
程序有一个帐目表,结构为
BSL(标识列)PZH(凭证号) CZJE(操作金额) BQJE(本期金额)
------- -------------- ------------------- ------------------
4 0000000000 5924619.3200 6047580.3200
5 0403240001 5011.2000 6052591.5200
6 0403240002 15.0000 6052606.5200
7 0403240003 12162.0000 6064768.5200
8 0403240004 5346.0000 6070114.5200
9 0403240005 8623.8000 6078738.3200
10 0403240006 6280.0000 6085018.3200
11 0403240007 12384.0000 6097402.3200
12 0403240008 6000.0000 6103402.3200
13 0403240009 3060.0000 6106462.3200

每条记录的(本期金额)都是上一条记录的(本期金额)加上本次的(操作金额)所得
由于开始的时候有几条记录的(操作金额)搞错了,所以导致以后的(本期金额)全部错误,现在出错的几笔(操作金额)已经改过来了,想把(本期金额)也改过来,如果用语句时间的话应该怎么写啊?先谢了!解决后另开贴答谢!
...全文
34 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
duck22 2004-05-10
  • 打赏
  • 举报
回复
高手如云
liang922 2004-05-10
  • 打赏
  • 举报
回复
学习
zicxc 2004-05-10
  • 打赏
  • 举报
回复
正版货是牛,要不牛就没人冒牌了

要是原表不按BSL排序,就是聚集索引不是BSL或者没有聚集索引

zjcxc 元老 2004-05-10
  • 打赏
  • 举报
回复
--测试

--测试数据
create table 帐目表(BSL int,PZH char(10),CZJE money,BQJE money)
insert 帐目表 select 4,'0000000000',5924619.3200,6047580.3200
union all select 5 ,'0403240001',5011.2000 ,6052591.5200
union all select 6 ,'0403240002',15.0000 ,6052606.5200
union all select 7 ,'0403240003',12162.0000 ,6064768.5200
union all select 8 ,'0403240004',5346.0000 ,6070114.5200
union all select 9 ,'0403240005',8623.8000 ,6078738.3200
union all select 10,'0403240006',6280.0000 ,6085018.3200
union all select 11,'0403240007',12384.0000 ,6097402.3200
union all select 12,'0403240008',6000.0000 ,6103402.3200
union all select 13,'0403240009',3060.0000 ,6106462.3200
go

--如果第一条本期金额是对的,应该改用:
--更新处理
declare @id int,@i money

select top 1 @i=BQJE,@i=BSL
from 帐目表 order by BSL

update 帐目表 set @i=@i+CZJE,BQJE=@i
where BSL<>@id
go

--显示处理结果
select * from 帐目表
go

--删除测试
drop table 帐目表

/*--测试结果

BSL PZH CZJE BQJE
----------- ---------- --------------------- ---------------------
4 0000000000 5924619.3200 6047580.3200
5 0403240001 5011.2000 6052591.5200
6 0403240002 15.0000 6052606.5200
7 0403240003 12162.0000 6064768.5200
8 0403240004 5346.0000 6070114.5200
9 0403240005 8623.8000 6078738.3200
10 0403240006 6280.0000 6085018.3200
11 0403240007 12384.0000 6097402.3200
12 0403240008 6000.0000 6103402.3200
13 0403240009 3060.0000 6106462.3200

(所影响的行数为 10 行)
--*/
zjcxc 元老 2004-05-10
  • 打赏
  • 举报
回复
--如果第一条本期金额是对的,应该改用:
--更新处理
declare @id int,@i money

select top 1 @i=BQJE,@i=BSL
from 帐目表 order by BSL

update 帐目表 set @i=@i+CZJE,BQJE=@i
where BSL<>@id
internetcsdn 2004-05-10
  • 打赏
  • 举报
回复
正版货才牛
pbsql 2004-05-10
  • 打赏
  • 举报
回复
update 帐目表 set BQJE=CZJE+isnull((select top 1 BQJE from 帐目表 t where t.BSL<帐目表.BSL order by t.BSL desc),0)
internetcsdn 2004-05-10
  • 打赏
  • 举报
回复
select sbl,bqje=(select sum(bqje) into #t from 帐目表 where sbl<=a.sbl) from 帐目表 a
update 帐目表 a set bqje=sqje from #t b where a.sbl=b.sbl
zjcxc 元老 2004-05-10
  • 打赏
  • 举报
回复
--测试

--测试数据
create table 帐目表(BSL int,PZH char(10),CZJE money,BQJE money)
insert 帐目表 select 4,'0000000000',5924619.3200,6047580.3200
union all select 5 ,'0403240001',5011.2000 ,6052591.5200
union all select 6 ,'0403240002',15.0000 ,6052606.5200
union all select 7 ,'0403240003',12162.0000 ,6064768.5200
union all select 8 ,'0403240004',5346.0000 ,6070114.5200
union all select 9 ,'0403240005',8623.8000 ,6078738.3200
union all select 10,'0403240006',6280.0000 ,6085018.3200
union all select 11,'0403240007',12384.0000 ,6097402.3200
union all select 12,'0403240008',6000.0000 ,6103402.3200
union all select 13,'0403240009',3060.0000 ,6106462.3200
go

--更新处理
declare @i money
set @i=0
update 帐目表 set @i=@i+CZJE,BQJE=@i
go

--显示处理结果
select * from 帐目表
go

--删除测试
drop table 帐目表

/*--测试结果

BSL PZH CZJE BQJE
----------- ---------- --------------------- ---------------------
4 0000000000 5924619.3200 5924619.3200
5 0403240001 5011.2000 5929630.5200
6 0403240002 15.0000 5929645.5200
7 0403240003 12162.0000 5941807.5200
8 0403240004 5346.0000 5947153.5200
9 0403240005 8623.8000 5955777.3200
10 0403240006 6280.0000 5962057.3200
11 0403240007 12384.0000 5974441.3200
12 0403240008 6000.0000 5980441.3200
13 0403240009 3060.0000 5983501.3200

(所影响的行数为 10 行)
--*/
zjcxc 元老 2004-05-10
  • 打赏
  • 举报
回复
--更新处理
declare @i money
set @i=0
update 帐目表 set @i=@i+CZJE,BQJE=@i
zicxc 2004-05-10
  • 打赏
  • 举报
回复
你这里第一条的本期金额就就不等于操作金额,如果第一条的本期金额是对的(不更新,而且以后一这个为准)

update a
set BQJE=(select top 1 BQJE from 帐目表 order by BSL)+(select sum(CZJE) from 帐目表 where BSL<=a.BSL and BSL<>(select min(BSL) from 帐目表))
from 帐目表 a
where BSL<>(select min(BSL) from 帐目表)

zicxc 2004-05-10
  • 打赏
  • 举报
回复
update a
set BQJE=(select sum(CZJE) from 帐目表 where BSL<=a.BSL)
from 帐目表 a
冷箫轻笛 2004-05-10
  • 打赏
  • 举报
回复
internetcsdn(公元1979年8月10日) 跟 pbsql(风云) 的我还没有试
因为其实我的表结构要比上面的复杂的多,套用起来挺麻烦。呵呵,慢慢再试了!

多谢各位了!
冷箫轻笛 2004-05-10
  • 打赏
  • 举报
回复
成功,揭贴了!

zjcxc(邹建) 进
http://expert.csdn.net/Expert/topic/3049/3049593.xml?temp=.5608789

DontWorry 2004-05-10
  • 打赏
  • 举报
回复
-----------------------------------------
declare @id int,@i money

....

update 帐目表 set @i=@i+CZJE,BQJE=@i
where BSL<>@id
-----------------------------------------

set @i=@i+CZJE,BQJE=@i


牛,又學了點東西, 請問還有類似的xiao技巧麼?
冷箫轻笛 2004-05-10
  • 打赏
  • 举报
回复
谢谢各位
谢谢 zjcxc(邹建) ,佩服一下!

调试一下后给分

34,575

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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