关于累计查询的问题的解决思路(重新讨论一个看似简单的查询)

火龙岛主 2006-04-10 02:48:29
原始帖子:http://community.csdn.net/Expert/topic/4613/4613168.xml?temp=.8729975
问题:
各位大虾:
表:
索引 凭证编码 金额 摘要 日期
cnsy pzbm pzje cnzy cnrq
1 101 100.00
2 102 100.00
3 202 200.00
4 203 100.00

得到结果
索引 凭证编码 期出余额 金额 期末金额 摘要
1 101 0 100.00 100.00
2 102 100.00 100.00 200.00
3 202 200.00 200.00 400.00
4 203 400.00 100.00 500.00

谢谢!

不要存储过程+函数+游标,因为要在Access里面执行

libin_ftsafe(子陌红尘)大虾给的答案

ACCESS里用这条语句:
------------------------------------------------------
select
a.cnsy as 索引,
a.pzbm as 凭证编码,
0 as 期出余额,
a.pzje as 金额,
a.pzje as 期末金额,
a.cnzy as 摘要
from
t a
where
not exists(select 1 from t where cnsy<a.cnsy)
union all
select
a.cnsy as 索引,
a.pzbm as 凭证编码,
sum(b.pzje) as 期出余额,
a.pzje as 金额,
sum(b.pzje)+a.pzje as 期末金额,
a.cnzy as 摘要
from
t a,t b
where
a.cnsy>b.cnsy
group by
a.cnsy,a.pzbm,a.pzje,a.cnzy

问题是:当删除中间日期的时候,再插入新的旧日期数据,按照日期排序,数值就不正确了!
小弟经验有限,不知各位大虾有何看法?有什么思路?
...全文
211 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
火龙岛主 2006-04-12
  • 打赏
  • 举报
回复
其它网友的我没有测试,
个人认为libin_ftsafe(子陌红尘) 的思路清晰,容易理解!
有可能根个人的习惯有关!
火龙岛主 2006-04-12
  • 打赏
  • 举报
回复
感谢libin_ftsafe(子陌红尘) 大虾的帮助,问题也成功解决了!也对其他同仁的参与表示感谢!谢谢大家:其实这是个典型的财务帐务处理问题!在财务里经常能用到!
火龙岛主 2006-04-12
  • 打赏
  • 举报
回复
回答gaojier1000(青岛※高捷) :作财务的时候就需要!
子陌红尘 2006-04-11
  • 打赏
  • 举报
回复
select
a.cnsy as 索引,
a.pzbm as 凭证编码,
0 as 期出余额,
a.pzje as 金额,
a.pzje as 期末金额,
a.cnzy as 摘要
from
t a
where
not exists(select 1 from t where cnrq<a.cnrq)
union all
select
a.cnsy as 索引,
a.pzbm as 凭证编码,
sum(b.pzje) as 期出余额,
a.pzje as 金额,
sum(b.pzje)+a.pzje as 期末金额,
a.cnzy as 摘要
from
t a,t b
where
a.cnrq>b.cnrq
group by
a.cnsy,a.pzbm,a.pzje,a.cnzy
  • 打赏
  • 举报
回复
Access需要这么复杂的处理吗?
十一月猪 2006-04-11
  • 打赏
  • 举报
回复
select cnsy as 索引,
pzbm as 凭证编码,
(
select isnull(sum(pzje) ,0)
from @t a
where a.cnrq< b.cnrq
) as 期出余额,
pzje as 金额,
(
select isnull(sum(pzje) ,0)
from @t a
where a.cnrq<= b.cnrq
) 期末金额
from @t b
十一月猪 2006-04-11
  • 打赏
  • 举报
回复
declare @t table(cnsy int , pzbm int , pzje dec(10,2) )
insert into @t
select 1 , '101', 100.00 union
select 2 , '102', 100.00 union
select 3 , '202', 200.00 union
select 4 , '203', 100.00
select * from @t


select cnsy as 索引,
pzbm as 凭证编码,
(
select isnull(sum(pzje) ,0)
from @t a
where a.cnsy < b.cnsy
) as 期出余额,
pzje as 金额,
(
select isnull(sum(pzje) ,0)
from @t a
where a.cnsy <= b.cnsy
) 期末金额
from @t b
Yang_ 2006-04-11
  • 打赏
  • 举报
回复
select *,
isnull((select sum(pzje) from @t
where cnrq < a.cnrq or (cnrq = a.cnrq and cnsy < a.cnsy)),0) 期出余额,
(select sum(pzje) from @t
where cnrq < a.cnrq or (cnrq = a.cnrq and cnsy <= a.cnsy)) 期末金额 from @t a
子陌红尘 2006-04-11
  • 打赏
  • 举报
回复
select
a.cnsy as 索引,
a.pzbm as 凭证编码,
0 as 期出余额,
a.pzje as 金额,
a.pzje as 期末金额,
a.cnzy as 摘要
from
t a
where
not exists(select 1 from t where cnrq<a.cnrq)
and
not exists(select 1 from t where cnrq=a.cnrq and cnsy<a.cnsy)
union all
select
a.cnsy as 索引,
a.pzbm as 凭证编码,
sum(b.pzje) as 期出余额,
a.pzje as 金额,
sum(b.pzje)+a.pzje as 期末金额,
a.cnzy as 摘要
from
t a,t b
where
a.cnrq>b.cnrq or (a.cnrq=b.cnrq and a.cnsy>b.cnsy)
group by
a.cnsy,a.pzbm,a.pzje,a.cnzy
火龙岛主 2006-04-11
  • 打赏
  • 举报
回复
这样新的问题又出现了,如果同时有很多相同日期的记录
就不能正确计算期初金额了!
还望大虾多多费心!谢谢!
火龙岛主 2006-04-10
  • 打赏
  • 举报
回复
排序以日期时间为准,不已索引为准,
原因是当某个凭证作废后,重新生成的数据就排到后面去了,这样计算出的期初数就不正确了!
viptiger 2006-04-10
  • 打赏
  • 举报
回复
select *,
isnull((select sum(pzje) from @t b
where a.cnsy > b.cnsy),0) 期出余额,
(select sum(pzje) from @t b
where a.cnsy >= b.cnsy)期末金额 from @t a
子陌红尘 2006-04-10
  • 打赏
  • 举报
回复
排序是以索引号的顺序为准吗?
sxdoujg 2006-04-10
  • 打赏
  • 举报
回复
up

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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