数据库有以下字段:
入库表:(vdate入库日期,nb为物料编号,name为物料名称,amount为入库数量)
vdate        nb         name       amount
2005-04-01   001        原珠笔     1000
2005-05-01   002        水性笔     3000
2005-05-01   001        原珠笔     2000出库表:(odate出库日期,nb为物料编号,name为物料名称,amount出库数量)
odate        nb         name        amount
2005-04-01   001        原珠笔      100
2005-05-01   002        水性笔      100
2005-05-02   001        原珠笔      200现在想求出2005年5月1日至2005年5月30日的库存表:
(综合性很强的罗!)详细如下:
注:
上次库存是(2005年5月1日以前的入库数量)-(2005年5月1日以前的出库数量)
本月入库是(2005年5月1日至2005年5月30日的入库数量)
本月出库是(2005年5月1日至2005年5月30日的出库数量)
还剩库存是(上次库存+本月入库-本月出库)物料编号   物料名称  上次库存   本月入库  本月出库  还剩库存
001        原珠笔     900         2000       200      2700
002        水性笔     0           3000       100      2900
帮忙一下,谢谢

解决方案 »

  1.   

    我觉得用SQL可以直接算出。。
    那位,来一句。。谢谢!
      

  2.   

    你是贴全了
    我岂不是还要在sql server里面create table...
    insert into table...
    这个很烦的阿
      

  3.   

    谢谢你连发三贴。
    我就是对JOIN不熟啊。。
    期待中!
      

  4.   

    什么数据库阿楼主难道不能把create table
    insert into
    贴出来么
      

  5.   

    icedut(冰) 说的没错,你现在的表是多对多关系。我想这样实现起来简单一点
      

  6.   

    谢谢两位。
    SQL SERVER2000的
      

  7.   

    create table tmp_input(vdate datetime,nd varchar(20),name varchar(20),amount int )
    create table tmp_output(vdate datetime,nd varchar(20),name varchar(20),amount int )insert into tmp_input
    select '2005-04-01','001','原珠笔',1000
    union
    select '2005-05-01','002','水性笔', 3000
    union
    select '2005-05-01','001','原珠笔', 2000insert into tmp_output
    select '2005-04-01','001','原珠笔',100
    union
    select '2005-05-01','002','水性笔',100
    union
    select '2005-05-02','001','原珠笔',200--某一个时间点的库存
    select nd,name,sum(case type when 'in' then amount else 0 end)-sum(case type when 'out' then amount else 0 end)
    from 
    (
    select nd,name,vdate,amount,'in' as Type from tmp_input
    union 
    select nd,name,vdate,amount,'out' as type from tmp_output
    )
    a where vdate<='2005-05-30'
    group by nd,name
      

  8.   

    你现在会有一个问题
    就是一个产品本月入库表中有,出库表中没有,或者上个月有这个月没有,等等类似这样的情况
    所以要象 icedut(冰) 所说的,要有一个产品表来解决这样的问题
      

  9.   

    高手啊,这么快。
    select nd,name,vdate,amount,'in' as Type from tmp_input
    'in' as Type 是什么东东啊?
      

  10.   

    通俗地说查询后多出一列别名为Type,内容是in,应该是表示入库的意思吧
      

  11.   

    用in 表示入库
    type 表示类型列as type   相当于生成一个列名为type的列
      

  12.   

    谢谢。
    等下,我两张表的结构不一样,这样用UNION好像不行吧?
      

  13.   

    谢谢。
    等下,我两张表的结构不一样,这样用UNION好像不行吧?--
    我取得字段结构是一样的不就行了么
      

  14.   

    和我这几天帮人做的差不多,你应该还有一个库存表吧。这样就可以很容易操作了,而且这样打印入库,出库也很方便啊,我做库存管理建了4个表,出入,库存,用户,你要查库存就直接来比较就是了,或者通过filter来实现都可以,不需要那么麻烦的,而且效率提高
      

  15.   

    --某一个时间点的库存
    select nd,name,sum(case type when 'in' then amount else 0 end)-sum(case type when 'out' then amount else 0 end)
    from 
    (
    select nd,name,vdate,amount,'in' as Type from tmp_input
    union 
    select nd,name,vdate,amount,'out' as type from tmp_output
    )
    a where vdate<='2005-05-30'
    group by nd,name不行啊,这只能仅仅是库存
    能不能一步到位就有:
    物料编号   物料名称  上次库存   本月入库  本月出库  还剩库存谢谢你。。
      

  16.   

    select a.物料编号   a.物料名称  a,上次库存   b.本月入库  b.本月出库  c.还剩库存 from
    ....
    where a.物料编号 =b.物料编号 and  a.物料编号 =c.物料编号
      

  17.   

    select a.物料编号   a.物料名称  a,上次库存   b.本月入库  b.本月出库  c.还剩库存 from
    ....
    where a.物料编号 =b.物料编号 and  a.物料编号 =c.物料编号
      

  18.   

    select nd,name,sum(amount) as InQty 
    from tmp_input
    where   vdate>='2005-05-01' and vdate<='2005-05-31'        --本月入
    group by nd,nameselect nd,name,sum(amount) as OutQty 
    from tmp_output
    where   vdate>='2005-05-01' and vdate<='2005-05-31'        --本月出
    group by nd,name
      

  19.   

    呵呵,你来了。
    不行啊,我觉得如果能用SQL语句一步取出来,这是最好的。
    不然太多事了。
      

  20.   

    --没有上月库存,写得好麻烦啊,谁有好的给写写
    select d.nd,d.name,d.Stock,e.inQty,f.outqty
    from (
    select nd,name,
    sum(case type when 'in' then amount else 0 end)-sum(case type when 'out' then amount else 0 end) as Stock
    from 
    (
    select nd,name,vdate,amount,'in' as Type from tmp_input
    union 
    select nd,name,vdate,amount,'out' as type from tmp_output
    )
    a where vdate<='2005-05-30'
    group by nd,name
    ) d
    left outer join
    (
    select nd,name,sum(amount) as InQty 
    from tmp_input
    where   vdate>='2005-05-01' and vdate<='2005-05-31'
    group by nd,name
    ) e on d.nd=e.nd
    left outer join 
    (
    select nd,name,sum(amount) as OutQty 
    from tmp_output
    where   vdate>='2005-05-01' and vdate<='2005-05-31'
    group by nd,name
    )f on d.nd=f.nd
      

  21.   

    楼主,这样的效率肯定很差
    select d.nd,d.name,d.Stock,e.inQty,f.outqty,g.stock as PrevStock
    from (
    select nd,name,
    sum(case type when 'in' then amount else 0 end)-sum(case type when 'out' then amount else 0 end) as Stock
    from 
    (
    select nd,name,vdate,amount,'in' as Type from tmp_input
    union 
    select nd,name,vdate,amount,'out' as type from tmp_output
    )
    a where vdate<='2005-05-30'
    group by nd,name
    ) d
    left outer join
    (
    select nd,name,sum(amount) as InQty 
    from tmp_input
    where   vdate>='2005-05-01' and vdate<='2005-05-31'
    group by nd,name
    ) e on d.nd=e.nd
    left outer join 
    (
    select nd,name,sum(amount) as OutQty 
    from tmp_output
    where   vdate>='2005-05-01' and vdate<='2005-05-31'
    group by nd,name
    )f on d.nd=f.ndleft outer join
    (
    select nd,name,
    sum(case type when 'in' then amount else 0 end)-sum(case type when 'out' then amount else 0 end) as Stock
    from 
    (
    select nd,name,vdate,amount,'in' as Type from tmp_input
    union 
    select nd,name,vdate,amount,'out' as type from tmp_output
    )
    g1 where vdate<='2005-04-30'
    group by nd,name
    ) g
    on d.nd=g.nd
      

  22.   

    好强啊。
    icedut,谢谢你,按照你的代码,我先研究一下去。。
      

  23.   

    帮你顶,我也在写进销存的东东,刚开始达建枢架,希望交流
    MSN: [email protected]
      

  24.   

    看得出,各位都是专家,在下有礼了!txtSQL = "select * from sufferExpand where [time] between # 2001-01-01# and #2005-06-03# "
    我是一个VB初学者,
    请教怎样写一个的语句,使用变量代替语句中的日期?
      

  25.   

    "select * from sufferExpand where [time] between #" & 变量1 & "# and #" & 变量2 & "# "