我有四个表orderinfo、表InputMateriel、表OutputMateriel、表StockPileInfo
-----------InputMateriel表结构如下:---------------------订料表
类型    供应商名 订料单号 订料名称   数量     要求到料时间 实际到料时间
ordtype proname  ordmcode orddsMname ordNumber   ordSqdata   ordsjdata
-----------InputMateriel表结构如下:---------------------来料表
类型    来料时间 来料单号 料名,供应商,来料数  
Ipttype mdata    Iptmcode mname pname   Iptmnum 
-----------OutputMateriel表结构如下:--------------------发料表
类型    来料时间 来料单号 料名,供应商,来料数  
Opttype mdata    Optmcode mname pname   Optmnum  
-----------StockPileInfo表结构如下:---------------------库存表
类型  料名   库存数
Stotype mname  Stomnum   进料之前此表是空表
-------------------------------------------------------
原始数据
*************************************************************************
select * from OrderInfo
结果:类型 供应商名 订料单号 订料名称  数量   要求到料时间 实际到料时间
      1001 一厂     A-1001   电源      200    2007-04-10   2007-04-12
      1001 一厂     A-1002   电容      100    2007-04-12   2007-04-12 
      1001 一厂     A-1003   电源      500    2007-04-18   2007-04-19select * from InputMateriel 
   结果:类型  料名 供应商 来料单号 来料时间   来料数 
        1001  电源  一厂  AL-1001   2007-04-12  200    
        1001  电容  一厂  AL-1002   2007-04-15  100
        1001  电源  一厂  AL-1003   2007-04-19  500        
select * from OutputMateriel 
   结果:类型  料名 供应商 发料单号 发料时间  发料数
        1001  电源  一厂  AF-1001   2007-04-13  100
        1001  电源  一厂  AF-1002   2007-04-15  30
        1001  电源  一厂  AF-1003   2007-04-16  20
        1001  电容  一厂  AF-1004   2007-04-16  50
        1001  电容  一厂  AF-1005   2007-04-18  20select * from StockPileInfo (此表实时更新,来一批料现库存+来料数,发一批料现库存-发料数)
结果:类型 料名 现库存  
     1001  电源 550  
     1001  电容 30
-------------------------------------------------------------------------
注释:1、(来料时间=实际到料时间)2、(现在库存=原库存+来料数-发料数)
一种类型可以有很多种料...
-------------------------------------------------------------------------
怎么按  类型、料名、时间结合四个表查询得到如下结果:条件是按 (型号+料名+时间)查询
1、****(具体细化查询) 假如按 类型为:1001  
                          料名为:电源
                          时间为: between '2007-04-11' and '2007-04-20'
                          排序  : 来料时间 发料时间
得到的结果如下:
订料单号类型料名来料单号 来料时间 来料数量 发料单号 发料时间 发料数量 库存
A-1001 1001 电源 AL-1001 2007-04-12  200    NULL      NULL      NULL  200
A-1001 1001 电源 NULL    NUL NULL   AF-1001   2007-04-13 100  100
A-1001 1001 电源 NULL    NULL NULL   AF-1002   2007-04-15 30   70
A-1001 1001 电源 NULL    NULL NULL   AF-1003   2007-04-16 20   50
A-1001 1001 电源 AL-1003 2007-04-19  500    NULL      NULL      NULL  550
{           来发总数           }     700    NULL      NULL      150   550

解决方案 »

  1.   

    select * from (
    select o.订料单号,i.类型,i.料名,i.来料单号,i.来料时间,i.来料数 as 来料数量,cast(null as varchar(20)) as 发料单号,cast(null as datetime) as 发料时间,cast(null as int) as 发料数量,(select sum(库存) from (select 来料数 as 库存 from InputMateriel where 类型=i.类型 and 料名=i.料名 and 来料时间<=i.来料时间 union all select -发料数 as 库存 from OutputMateriel where 类型=i.类型 and 料名=i.料名 and 发料时间<i.发料时间) as t) as 库存
    from InputMateriel i left join OrderInfo o
    on i.类型=o.类型 and i.料名=o.料名 and i.来料时间=o.实际到料时间
    union all
    select null as 订料单号,i.类型,i.料名,null as 来料单号,null as 来料时间,null as 来料数量,发料单号,发料时间,发料数 as 发料数量,(select sum(库存) from (select 来料数 as 库存 from InputMateriel where 类型=i.类型 and 料名=i.料名 and 来料时间<=i.来料时间 union all select -发料数 as 库存 from OutputMateriel where 类型=i.类型 and 料名=i.料名 and 发料时间<=i.发料时间) as t) as 库存
    from OutputMateriel i
    ) as t1
    order by 类型,料名,isnull(来料时间,发料时间)
      

  2.   

    Yang_
    你好!谢谢你...
      

  3.   

    Yang_
    你好!谢谢你...
    你自己有没有测试过啊?要是测试过的话
    可不可以将你 create table
                 insert into table  等
    一起帖上来啊?因为我的字段和你的不一样,我的是英文的