寻求算数据总和的最好解决方法?!

yhd0411 2003-09-25 05:10:32
表vouch
date char 8 (日期)
lsh char 6  (流水号)
acno char 16  (帐号) 
amnt decimal   (余额)

数据有
date lsh acno amnt
20030514 160001 9011500201000088 10000.00
20030514 160002 9011500201000088 200.00
20030618 150001 9011500201000088 1500.00
20030711 120005 9011500201000088 20000.00
20030711 120007 9011500201000088 35000.00
.......

要求:
按帐号每日取一笔数据(按流水号排列)后的最后一笔余额
然后每日累加,当天没有发生取上日的余额。

上面数据(帐号9011500201000088):
1、20030514,取流水号为16002的余额为:200.00
第二日没有发生:20030515,余额也为:200.00
....................
以此到20030617余额也为:200.00
累加结果:200*34=6800.00

2、20030618余额为:1500.00
一直到20030710,累加结果:1500*23=34800.00

3、20030711取流水号为12007的余额为:35000.00

4、到20030711日终结果为6800+34800+3500=45100

能否在Sql server中实现?
或者用VB编程实现?

寻求高效率的,因为数据太大!


...全文
55 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
zjcxc 元老 2003-09-25
  • 打赏
  • 举报
回复
下面是数据测试:

--定义数据测试环境
declare @vouch table(date char(8),lsh char(6),acno char(16),amnt decimal(20,2))
insert into @vouch
select '20030514','160001','9011500201000088',10000.00
union all select '20030514','160002','9011500201000088',200.00
union all select '20030618','150001','9011500201000088', 1500.00
union all select '20030711','120005','9011500201000088',20000.00
union all select '20030711','120007','9011500201000088',35000.00
union all select '20030811','120007','9011500201000089',35000.00

--生成数据处理临时表
select id=identity(int,0,1),* into #tb
from(
select acno,date,amnt,ye=cast(0 as decimal(20,2))
from @vouch a
where lsh=(select max(lsh) from @vouch where date=a.date and acno=a.acno)
union all
select acno,convert(char(8),convert(datetime,max(date),112)+1,112),0,0 from @vouch group by acno
) a order by acno,date,amnt desc

--处理数据
declare @acno char(16),@date char(8),@ye decimal(20,2),@yesum decimal(20,2)

update #tb set @yesum=case acno when @acno then @yesum+@ye*(datediff(day,@date,date)) else 0 end
,@ye=amnt,@acno=acno,@date=date
,ye=@yesum
from #tb

--显示结果
select acno,余额=ye from #tb a where id=(select max(id) from #tb where acno=a.acno)

--删除数据处理临时表
drop table #tb

/*--下面是在我的电脑上的测试结果--*/
acno 余额
---------------- ----------------------
9011500201000089 35000.00
9011500201000088 76500.00

(所影响的行数为 2 行)
--*/
zjcxc 元老 2003-09-25
  • 打赏
  • 举报
回复
用下面的方法就行了.

--生成数据处理临时表
select id=identity(int,0,1),* into #tb
from(
select acno,date,amnt,ye=cast(0 as decimal(20,2))
from vouch a
where lsh=(select max(lsh) from vouch where date=a.date and acno=a.acno)
union all
select acno,convert(char(8),convert(datetime,max(date),112)+1,112),0,0 from vouch group by acno
) a order by acno,date,amnt desc

--处理数据
declare @acno char(16),@date char(8),@ye decimal(20,2),@yesum decimal(20,2)

update #tb set @yesum=case acno when @acno then @yesum+@ye*(datediff(day,@date,date)) else 0 end
,@ye=amnt,@acno=acno,@date=date
,ye=@yesum
from #tb

--显示结果
select acno,余额=ye from #tb a where id=(select max(id) from #tb where acno=a.acno)
subbee 2003-09-25
  • 打赏
  • 举报
回复
up

34,874

社区成员

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

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