库存统计的SQL语句
使用到三个表:
1. 入库表:w_StockBillDetail(有入库类型) 有品名MaterialID_Name,规格Specification,单位,数量(Packages)
2. 送货单表:d_DeliveryBillDetail (明细) 有品名ComdName,规格Specification,单位Unit,数量Quantity
3. 外发表:w_outWardDetail(外发明细) 有品名GoodsName,规格Specs,单位,数量Amount
送货单表与入库表通过产品名称和规格来关联
--跟踪:
--开订单
select innerid,* from c_custOrder where billno='D090928-000155' --订单号是billno
select * from c_custOrderdetail where innerid='753' --通过innerid键关联
--开生产单(现在订单与生产单还没有关联起来)
select CustOrderInnerID_BillNo,CustOrderInnerID,OldInnerID,*
from p_productionbill where CustOrderInnerID_BillNo='D090928-000155' --通过OldInnerID 键与订单号关联,CustOrderInnerID_BillNo
-- B090928-000397 是生产单号
--开送货单
select * from d_DeliveryBill where billno='Z090928-000107'
select * from d_DeliveryBillDetail where ProductionID_BillNo='B090928-000397' and innerid='113' --ProductionID_BillNo是生产单号,通过生产单号与生产表相连
--入库
select * from w_stockbill
select * from w_stockbilldetail
--外发
select * from w_outward
select * from w_outwarddetail ---通过innerid与主表w_outward关联
----------------------------------开始编写 以下语句可运行在Zefu数据库上
select wsde.MaterialID_Name pm,wsde.Specification gg, bi.name as rklx, bu.name as danwei,
IsNull(sum(wsde.Packages),0) as rksl, sum(IsNull(wode.Amount,0)) as wfsl, IsNull(sum(wdde.Quantity),0) as shsl,
IsNull((IsNull(sum(wsde.Packages),0) - sum(IsNull(wode.Amount,0)) - IsNull(sum(wdde.Quantity),0)),0) as kcsl
from w_stockbilldetail wsde
left join b_unit bu on wsde.unit = bu.innerid --单位基本表
left join w_stockbill wst on wst.innerid = wsde.innerid --入库类型
left join b_instoretype bi on wst.storetype = bi.innerid --入库类型基本表
left join w_outwarddetail wode on wode.GoodsName = wsde.MaterialID_Name and wode.Specs = wsde.Specification --外发单通过品名,规格与入库相关联
left join D_DeliveryBilldetail wdde on wdde.ComdName = wsde.MaterialID_Name and wdde.Specification = wsde.Specification
group by wsde.MaterialID_Name, wsde.Specification, bu.name,bi.name
--select * from b_unit 单位名称
--select * from b_instoretype 入库类型
--select GoodsName,* from w_outward 外发出库
--select GoodsName,* from w_outwarddetail
--select Quantity,* from D_DeliveryBilldetail
-------------------------------------------------