我有四张桌子
我想要报告:
Date, ItemName, Opening Quantity, Received Quantity, Used Quantity, Balance
我没有收到结果的日期。
我申请了,用不同的表格(原创)
select *
FROM (SELECT ivt.ITEMID AS rItemID, ivt.NAMEALIAS as rItemName, iUOM, YearRec, MonthRec, OpeningStock, RecMaterial
FROM INVENTTABLE ivt LEFT JOIN
(SELECT in1.YearRec, in1.MonthRec, in1.rItemID, iUOM, ISNULL(in1.OpeningStock, 0) AS OpeningStock, ISNULL(in1.ReceivedMaterial,0) AS RecMaterial
FROM (SELECT Year(ReceivedDate) AS YearRec, Month(ReceivedDate) AS MonthRec, rItemID, iUOM, MIN(OpenQty) AS OpeningStock, SUM(RecQty) ReceivedMaterial
FROM ( SELECT rItemID, ReceivedDate, iUOM, SUM(ReceivedQty) AS RecQty, (SELECT SUM(q.QTY) FROM VENDPACKINGSLIPTRANS q WHERE q.INVENTDATE < rm.ReceivedDate AND q.ITEMID = rm.rItemID) AS OpenQty
FROM ( SELECT vp.ITEMID AS rItemID, vp.INVENTDATE as ReceivedDate, vp.PURCHUNIT as iUOM, SUM(vp.QTY) as ReceivedQty FROM VENDPACKINGSLIPTRANS AS vp GROUP BY vp.ITEMID, vp.INVENTDATE,vp.PURCHUNIT) rm
GROUP BY rItemID, ReceivedDate,iUOM) inv
GROUP BY Year(ReceivedDate), Month(ReceivedDate), rItemID, iUOM) in1 WHERE (in1.YearRec= 2016 AND in1.MonthRec = 06)
) iv2
ON ivt.ITEMID = iv2.rITemID) le
LEFT JOIN
(SELECT c1.YearConsume, c1.MonthConsume, c1.cItemID, c1.BOM_Unit, MIN(c1.BOM_Consumption_Prev) as PrevConsump, SUM(c1.BOM_ConsumptionQty_Curr) AS TotalConsump
FROM ( SELECT Year(DateConsume) AS YearConsume, Month(DateConsume) AS MonthConsume, ItemID as cITemID, BOM_Unit, ISNULL(BOM_Consumption_Open,0) AS BOM_Consumption_Prev, SUM(BOM_ConsumptionQty) AS BOM_ConsumptionQty_Curr
FROM ( SELECT DateConsume, ItemID, BOM_Unit, SUM(BOM_Consumption) AS BOM_ConsumptionQty,
( SELECT SUM(bc.BOM_Consumption) FROM (SELECT (b.TRANSDATE) AS DateConsume, b.ITEMID AS cItemID, b.BOMUNITID BOM_Unit, SUM(b.BOMCONSUMP) AS BOM_Consumption FROM PRODJOURNALBOM b GROUP BY b.TRANSDATE, b.ITEMID, b.BOMUNITID) bc
WHERE (bc.DateConsume < aa.DateConsume) AND bc.cItemID = aa.ItemID) AS BOM_Consumption_Open
FROM (SELECT (b.TRANSDATE) AS DateConsume, b.ITEMID AS ItemID, b.BOMUNITID BOM_Unit, SUM(b.BOMCONSUMP) AS BOM_Consumption FROM PRODJOURNALBOM b GROUP BY b.TRANSDATE, b.ITEMID, b.BOMUNITID) aa
GROUP BY DateConsume, ItemID, BOM_Unit) cc
GROUP BY Year(DateConsume), Month(DateConsume), ItemID,BOM_Unit, BOM_Consumption_Open) c1
WHERE (YearConsume= 2016 AND MonthConsume = 06)
GROUP BY c1.YearConsume, c1.MonthConsume, c1.cItemID, c1.BOM_Unit) rt
ON le.rItemID = rt.cITemID
WHERE le.rITEMID IN ('FML-RM006', 'FML-RM008','FML-RM016','FML-RM018')