| 发表于:2007-04-23 10:32:352楼 得分:100 |
drop table orderinfo,inputmateriel,outputmateriel,stockpileinfo go create table orderinfo(ordtype varchar(10),proname varchar(100),ordmcode varchar(20),orddsmname varchar(255),ordnumber numeric(20,6),ordsqdata datetime,ordsjdata datetime) create table inputmateriel(ipttype varchar(10),mdata datetime,iptmcode varchar(20),mname varchar(255),pname varchar(100),iptmnum numeric(20,6)) create table outputmateriel(opttype varchar(10),mdata datetime,optmcode varchar(20),mname varchar(255),pname varchar(100),optmnum numeric(20,6)) create table stockpileinfo(stotype varchar(10),mname varchar(255),stomnum numeric(20,6)) insert into orderinfo select '1001 ', '一厂 ', 'a-1001 ', '电源 ',200, '2007-04-10 ', '2007-04-12 ' union all select '1002 ', '二厂 ', 'b-1001 ', '电容 ',100, '2007-04-12 ', '2007-04-12 ' insert into inputmateriel(ipttype,mname,pname,iptmcode,mdata,iptmnum) select '1001 ', '电源 ', '一厂 ', 'al-1001 ', '2007-04-12 ',200 union all select '1002 ', '电容 ', '二厂 ', 'bl-1001 ', '2007-04-15 ',100 insert into outputmateriel(opttype,mname,pname,optmcode,mdata,optmnum) select '1001 ', '电源 ', '一厂 ', 'af-1001 ', '2007-04-13 ',100 union all select '1001 ', '电源 ', '一厂 ', 'af-1002 ', '2007-04-15 ',30 union all select '1001 ', '电源 ', '一厂 ', 'af-1003 ', '2007-04-16 ',20 union all select '1002 ', '电容 ', '二厂 ', 'af-1004 ', '2007-04-20 ',10 union all select '1002 ', '电容 ', '二厂 ', 'af-1005 ', '2007-04-21 ',50 insert into stockpileinfo select '1001 ', '电源 ',50 union all select '1002 ', '电容 ',40 select * from ( select b.ordmcode as '订料单号 ', a.mname as '料名 ', a.iptmcode as '来料单号 ', convert(char(10),a.mdata,120) as '来料时间 ', a.iptmnum as '来料数量 ', null as '发料单号 ', null as '发料时间 ', null as '发料数量 ', a.iptmnum as '库存 ' from inputmateriel a inner join orderinfo b on a.ipttype=b.ordtype and a.mname=b.orddsmname union all select b.ordmcode, c.mname, null, null, null, c.optmcode, convert(char(10),c.mdata,120), c.optmnum, isnull((select sum(isnull(optmnum,0))+x.stomnum from outputmateriel y where y.opttype=c.opttype and y.mname=c.mname and c.mdata <y.mdata),x.stomnum) from outputmateriel c inner join orderinfo b on c.opttype=b.ordtype and c.mname=b.orddsmname inner join stockpileinfo x on x.stotype=c.opttype and x.mname=c.mname)t order by 订料单号,料名,来料单号 desc /* 订料单号 料名 来料单号 来料时间 来料数量 发料单号 发料时间 发料数量 库存 -------------------------------------------------------------------------------------------- a-1001 电源 al-1001 2007-04-12 200.000000 null null null 200.000000 a-1001 电源 null null null af-1001 2007-04-13 100.000000 100.000000 a-1001 电源 null null null af-1002 2007-04-15 30.000000 70.000000 a-1001 电源 null null null af-1003 2007-04-16 20.000000 50.000000 b-1001 电容 bl-1001 2007-04-15 100.000000 null null null 100.000000 b-1001 电容 null null null af-1004 2007-04-20 10.000000 90.000000 b-1001 电容 null null null af-1005 2007-04-21 50.000000 40.000000 */ | | |
|