货品表 
代码  名称        期初数量  当前库存 
001  实验商品1  100      2 
单据主表 
单号            单据日期          
RK20080709    20080709        
CK20080710    20080709          
单据明细表 
单号      货品代码    数量      
RK20080709  001      2          
CK20080710  001      100        需要得到如下的报表 
查询日期段为 2008-07-09 到 2008-07-10  货品为 实验商品1 
开单日期  入库    出库    结存 
20080709  2    0      102 
20080710  0    100    2 数据库为 access 如何用sql实现 
关键是结存数量的计算 望各位大虾出手相助 
小弟在这儿多谢了

解决方案 »

  1.   


    DECLARE @GOODS TABLE(uID VARCHAR(5), uNAME VARCHAR(10), BegNum int, NowNum int)
    insert @goods select '001',  '实验商品1',  100,      1 
    insert @goods select '002',  '实验商品2',  10,      1 declare @Main table(uID varchar(10), uDate datetime)
    insert @main select 'RK20080709',    '20080709'        
    insert @main select 'CK20080710',    '20080709'
    insert @main select 'CK20080711',    '20080718'declare @detail table(umainID varchar(10),uGoodsID varchar(5), Num int)
    insert into @detail select 'RK20080709',  '001',      2          
    insert into @detail select 'CK20080710',  '001',      100  
    insert into @detail select 'CK20080710',  '002',      8   
    insert into @detail select 'CK20080711',  '002',      1   
    insert into @detail select 'CK20080711',  '001',      1   select c.udate, 
    RK = case when left(b.umainid,2) = 'RK' then b.num else 0 end,
    CK = case when left(b.umainid,2) = 'CK' then b.num else 0 end
    from @goods a
    left join @detail b on a.uid = b.ugoodsid
    left join @main c on c.uid = b.umainid
    where a.uname = '实验商品1'udate                   RK          CK
    ----------------------- ----------- -----------
    2008-07-09 00:00:00.000 2           0
    2008-07-09 00:00:00.000 0           100
    2008-07-18 00:00:00.000 0           1(3 行受影响)
    另:结余库存是一个即时或实时的值,查询时每一条记录都显示有意意吗?