关于本问题的求救:前题说明:
zwfckbb 出入库详细数据表 主要字段
  ckbh 出库编号
  year varchar
  month varchar
  cpbh 产品编号product 产品表,里面包含产品的大类名称
  cpbh 产品编号
  dl   大类名称
cpdl    产品大类分类表,包含大类序号和名称,
  xh 大类序号
  dl 大类名称本来写了以下查询
select  bb.ckbh,bb.year,tc.xh,p.dl,sum(bb.lastnum) as lastnum,sum(bb.rk) as rk,
sum(bb.chk) as chk
 from zwfckbb bb,product p,cpdl tc where bb.year='2000' and (bb.cpbh=p.cpbh) and (p.dl=tc.dl) 
group by bb.ckbh,bb.year,tc.xh,p.dl 
查出来对就的结果是:
出库编号  统计年份 序号 大类名称 年初结存 本年入库 本年出库但是如果这样查询 统计数据都是一年的,如2000年的
但是问题在:年初结存指的是上年12月份的lastnum数目,并不是本年lastnum的合计
请问本问题如何解决!主要回答人将同另一贴的80分得到140分值

解决方案 »

  1.   

    不知道是我理解的问题,还是你说的有问题
    1.你要什么样的结果?
    2.“但是问题在:年初结存指的是上年12月份的lastnum数目,并不是本年lastnum的合计”
      这句特别不明白?
      

  2.   

    我在原先的统计中,所有的列都用输入的年份按年度来进行合计
    但是年初结存即lastnum并不是指输入年份如2003的合计,而是2002年12月的合计
    即2002年12月的合计做为2003年的年初结存
    所以在2003数据时,
    年初结存是统计2002.12的数据,而其它为2003的合计
      

  3.   

    联合查询!
    select  bb.ckbh,bb.year,tc.xh,p.dl,sum(bb.lastnum) as lastnum,sum(bb.rk) as rk,
    sum(bb.chk) as chk
     from zwfckbb bb,product p,cpdl tc where bb.year='2000' and (bb.cpbh=p.cpbh) and (p.dl=tc.dl) 
    group by bb.ckbh,bb.year,tc.xh,p.dl 
    union all
    select  bb.ckbh,bb.year,tc.xh,p.dl,sum(bb.lastnum) as lastnum,sum(bb.rk) as rk,
    sum(bb.chk) as chk
     from zwfckbb bb,product p,cpdl tc where bb.year='1999' and (bb.cpbh=p.cpbh) and (p.dl=tc.dl) 
    group by bb.ckbh,bb.year,tc.xh,p.dl 
      

  4.   

    select table1.ckbh,table1.year,table1.xh,table1.dl,table2.lastnum from (select  bb.ckbh,bb.year,tc.xh,p.dl,sum(bb.rk) as rk,
    sum(bb.chk) as chk
     from zwfckbb bb,product p,cpdl tc where (bb.cpbh=p.cpbh) and (p.dl=tc.dl) 
    group by bb.ckbh,bb.year,tc.xh,p.dl)as table1 join
    (select  bb.ckbh,bb.year,tc.xh,p.dl,sum(bb.lastnum) as lastnum,
    from zwfckbb bb,product p,cpdl tc where bb.month='12' and (bb.cpbh=p.cpbh) and (p.dl=tc.dl) 
    group by bb.ckbh,bb.year,bb.Month,tc.xh,p.dl) as table2 on
    table1.ckbh=table2.ckbh and convert(int,table1.year)=convert(int,table2.year+1) and table1.xh=table2.xh and table1.dl=table2.dl