这样的统计有没有什么好方法?(87分)

  • 主题发起人 sixeight
  • 开始时间
S

sixeight

Unregistered / Unconfirmed
GUEST, unregistred user!
报表:
日期 商品 上期结存 入库 出库 结存
2002-11-1 内存条 190 20 10 200
2002-11-2 内存条 200 0 40 160
.....
这种结存应该怎样统计,
原始数据表中只有出入库表
 
什么数据库?
 
如统计某期(eg:2002。12。31)的可以这样统计
方法一:
1、先得到11月30日的结存数(上期结存):select 结存 from 报表 where 日期=2002-11-30;
2、统计12月当月所有的入库、出库汇总数:
select sum(入库),sum(出库) from 报表 where 日期<=2002-12-31 and
日期>=2002-12-1 ;
3、将以上sum(入库)-sum(出库)+上期结存即为12月31日的结存
方法二:
1、select max(日期) into rq from 报表 where 日期<2002-12-31;
2、select 结存 into jc from 报表 where 日期=rq;
3、select 入库,出库 into rk,ck from 报表 where rq=2002-12-31;
4、jc+rk-ck即为2002-12-31的结存数



 
to zlc_168:你的这个做法太死,我想他的意图肯定是根据一个日期范围来求查询结果的

完全可以在一个SQL语句就写出这个过程,只不过太长了,难得调试.

 
顶部