有3个表,字段如下:
1,产品基本信息表
产品ID,产品编号,产品名称2,入库单表
入库单ID,产品ID,入库单批次号,入库单价,入库数量,入库时间
注:入库单是按批次保存产品总库存量的,也就是说一种产品库存量会由多个批次的入库量累加组成3,出库单表
出库单ID,产品ID,出库数量,出库时间
注:一个产品的总出库量由所有次产品的出库单的出库量相加而成没有库存量的相应字段,一个商品的库存量由总入库量减去总出库量求得。现在想统计某些商品的在几段时间内的库存数,具体如下:指定条件有:开始时间,结束时间,商品名称要根据条件得到以下统计数据:商品名称,开始时的库存数量, 开始时的库存金额, 从开始时间到结束时间期间的的入库总数量和总金额, 从开始时间到结束时间期间的出库总数量和总金额,到结束时间的剩余总库存数量和总金额。如何实现?请各位不吝赐教,谢谢~!

解决方案 »

  1.   

    2,入库单表
    入库单ID,产品ID,入库单批次号,入库单价,入库数量,入库时间
    注:入库单是按批次保存产品总库存量的,也就是说一种产品库存量会由多个批次的入库量累加组成===================================================================这个表能不能用具体的数据举下例子?
    "入库单批次号"和"入库数量"有什么关系吗?
      

  2.   

    ===表和字段的别名
    1,产品基本信息表 proInfo
    产品ID proId,产品编号 proNo,产品名称 proName2,入库单表 inStore
    入库单ID inId,产品ID proId,入库单批次号 inNo,入库单价 inPrice,入库数量 inQuantity,入库时间 inDate
    注:入库单是按批次保存产品总库存量的,也就是说一种产品库存量会由多个批次的入库量累加组成3,出库单表 outStore
    出库单ID outId,产品ID proId,出库数量 outQuantity,出库时间 outDate
    注:一个产品的总出库量由所有次产品的出库单的出库量相加而成
    ==============sql==========================select oldAmount.proName 商品名称,
           oldAmount.inQuan 开始时的库存数量,
           oldAmount.inTotal 开始时的库存金额,
           inAmount.inQuan 入库总数量,
           inAmount.inTotal 入库总金额,
           outAmount.outQuan 出库总数量,
           outAmount.outTotal 出库总金额,
           oldAmount.inQuan+inAmount.inQuan-outAmount.outQuan 剩余总库存数量,
           oldAmount.inTotal+inAmount.inTotal-outAmount.outTotal 剩余总金额
      from (
            select p.proName,ist.inQuantity as inQuan,ist.inQuantity*count(*) as inTotal
              from proInfo p,inStore ist
             where p.proName = &proName
               and ist.inDate <= &startTime
               and ist.proId = p.proId
           )oldAmount,
           (
            select p.proName,ist.inQuantity as inQuan,ist.inQuantity*count(*) as inTotal
              from proInfo p,inStore ist
             where p.proName = &proName
               and ist.inDate >= &startTime
               and ist.inDate <= &endTime
               and ist.proId = p.proId
           )inAmount,
           (
            select p.proName,ost.outQuantity as outQuan,ost.outQuantity*count(*) as outTotal
              from proInfo p,outStore ost
             where p.proName = &proName
               and ost.outDate >= &startTime
               and ost.outDate <= &endTime
               and ost.proId = p.proId 
           )outAmount
     where oldAmount.proName = inAmount.proName
       and inAmount.proName = outAmount.proName;试试看~~~
      

  3.   

    看看这个语句
    看能不能有启示
    select to_char(cdate,'yyyy-mm-dd'),
    sum(case when to_number(to_char( cdate,'hh24' ))>0 and to_number(to_char( cdate,'hh24' ))<=8 then cnum else 0 end) "0~8",
    sum(case when to_number(to_char( cdate,'hh24' ))>21 and to_number(to_char( cdate,'hh24' ))<=23 then cnum else 0 end) "21~23"
    from t4
    group by to_char(cdate,'yyyy-mm-dd')
    这个是按统计每天0点到8点,21点到23,value的总和
      

  4.   

    枫の叶) 兄的sql对我很有启发,首先感谢你,结铁的时候一定给你加分!但我有一个问题,如果执行查询的时候没有出过库,也就是说结果集outAmount为空时,整个查询的结果集也为空,这样就显示不了其他的数据了啊~!请问怎么处理这个问题才好?也欢迎其他大虾给点意见
      

  5.   

    加一个左连接就可以了select oldAmount.proName 商品名称,
           oldAmount.inQuan 开始时的库存数量,
           oldAmount.inTotal 开始时的库存金额,
           inAmount.inQuan 入库总数量,
           inAmount.inTotal 入库总金额,
           outAmount.outQuan 出库总数量,
           outAmount.outTotal 出库总金额,
           oldAmount.inQuan+inAmount.inQuan-outAmount.outQuan 剩余总库存数量,
           oldAmount.inTotal+inAmount.inTotal-outAmount.outTotal 剩余总金额
      from (
            select p.proName,ist.inQuantity as inQuan,ist.inQuantity*count(*) as inTotal
              from proInfo p,inStore ist
             where p.proName = &proName
               and ist.inDate <= &startTime
               and ist.proId = p.proId
           )oldAmount,
           (
            select p.proName,ist.inQuantity as inQuan,ist.inQuantity*count(*) as inTotal
              from proInfo p,inStore ist
             where p.proName = &proName
               and ist.inDate >= &startTime
               and ist.inDate <= &endTime
               and ist.proId = p.proId
           )inAmount,
           (
            select p.proName,ost.outQuantity as outQuan,ost.outQuantity*count(*) as outTotal
              from proInfo p,outStore ost
             where p.proName = &proName
               and ost.outDate >= &startTime
               and ost.outDate <= &endTime
               and ost.proId = p.proId 
           )outAmount
     where oldAmount.proName = inAmount.proName
       and oldAmount.proName = outAmount.proName(+);