计算库存
出入库表
出入库单号
出入库日期
类型 0-出库 1-入库
仓库
出入库明细表
出入库单号
产品类别名
产品名
重量
单位
转库单
转库单号
转库日期
转出仓库
转入仓库
转库明细表
转库单号
产品类别
产品名
重量
单位
要求算出截止某一日期某产品某仓库的库存,结果的格式为 仓库名,产品类别名,产品名,单位,库存
出入库表
出入库单号
出入库日期
类型 0-出库 1-入库
仓库
出入库明细表
出入库单号
产品类别名
产品名
重量
单位
转库单
转库单号
转库日期
转出仓库
转入仓库
转库明细表
转库单号
产品类别
产品名
重量
单位
要求算出截止某一日期某产品某仓库的库存,结果的格式为 仓库名,产品类别名,产品名,单位,库存
create table in_out_entry(InOutID varchar(10),ProductType varchar(10),ProductName varchar(20),Wt decimal(5,2),Unit varchar(10))
create table transfer(ID varchar(10),TranDate datetime,FromWH varchar(10) ,ToWH varchar(10))
create table transfer_entry(TranID varchar(10) ,ProductType varchar(10),ProductName varchar(20),Wt decimal(5,2),Unit varchar(10))insert into in_out
select '1111111','2009-01-01',0,'WH1'
union all select '2222222','2008-12-31',1,'WH1'insert into in_out_entry
select '1111111','fruit','apple',300,'ton'
union all select '1111111','meat','pork',500,'ton'
union all select '2222222','fruit','apple',100,'ton'
union all select '2222222','meat','pork',200,'ton'insert into transfer
select '333333','2009-01-02','WH1','WH2'insert into transfer_entry
select '333333','fruit','apple',100,'ton'select producttype,productname,sum(tot) as inventory,unit,warehouse from
(select a1.producttype,a1.productname,(case when a.inouttype=0 then a1.wt else a1.wt*-1 end) as tot,a1.unit,a.inoutdate,a.warehouse
from in_out a left join in_out_entry a1 on a.id=a1.inoutid
union all
select a1.producttype,a1.productname,a1.wt*-1,a1.unit,a.TranDate,a.FromWH
from transfer a left join transfer_entry a1 on a.id=a1.TranID
union all
select a1.producttype,a1.productname,a1.wt,a1.unit,a.TranDate,a.toWH
from transfer a left join transfer_entry a1 on a.id=a1.TranID) x
where warehouse='WH1' and productname='apple' and inoutdate<='2009-01-01'
group by producttype,productname,unit,warehouse