我正在查看过去12个月的出货数据,并希望发运的成品总数和原材料对应部件。
我已经使用物料清单标题(其中包含相应的成品商品)加入了货件明细表,然后将物料清单头连接到物料清单详细信息以获取每个成品单元的所有原材料组件和数量。 / p>
ShipYear ShipMonth CLASS SHIPMENT_ID INTERNAL_SHIPMENT_LINE_NUM FG_ITEM FG_QTY RM_ITEM RM_QTY_PER_FG_UNIT TOTAL_RM_QTY
2013 6 SHADE CHIPS 9701316 25851201 PM9000015050 2 PM1000010932 2 4
2013 6 SHADE CHIPS 9701316 25851201 PM9000015050 2 PM1000010933 3 6
2013 6 SHADE CHIPS 9701316 25851201 PM9000015050 2 PM1000010934 1 2
2013 6 SHADE CHIPS 9701316 25851201 PM9000015050 2 PM1000010935 4 8
2013 6 SHADE CHIPS 9701316 25851201 PM9000015050 2 PM1000010936 1 2
2013 6 SHADE CHIPS 9701316 25851201 PM9000015050 2 PM1000010937 1 2
2013 6 SHADE CHIPS 9701316 25851201 PM9000015050 2 PM1000010938 1 2
2013 6 SHADE CHIPS 9701316 25851201 PM9000015050 2 PM1000010939 1 2
2013 6 SHADE CHIPS 9701316 25851202 PM9000015074 5 PM1000010932 4 20
2013 6 SHADE CHIPS 9701316 25851202 PM9000015074 5 PM1000010933 1 5
2013 6 SHADE CHIPS 9701316 25851202 PM9000015074 5 PM1000010934 3 15
2013 6 SHADE CHIPS 9701316 25851202 PM9000015074 5 PM1000010935 8 40
2013 6 SHADE CHIPS 9701638 25853677 PM9000015394 1 PM1000010932 1 1
2013 6 SHADE CHIPS 9701638 25853677 PM9000015394 1 PM1000010933 1 1
2013 6 SHADE CHIPS 9701638 25853677 PM9000015394 1 PM1000010934 1 1
2013 6 SHADE CHIPS 9701638 25853677 PM9000015394 1 PM1000010935 4 4
2013 6 SHADE CHIPS 9701638 25853677 PM9000015394 1 PM1000010936 1 1
2013 6 SHADE CHIPS 9701638 25853677 PM9000015394 1 PM1000010937 2 2
2013 6 SHADE CHIPS 9701638 25853677 PM9000015394 1 PM1000010938 3 3
2013 6 SHADE CHIPS 9701638 25853677 PM9000015394 1 PM1000010939 1 1
2013 6 SHADE CHIPS 9701639 25853678 PM9000015404 1 PM1000010932 7 7
2013 6 SHADE CHIPS 9701639 25853678 PM9000015404 1 PM1000010933 1 1
2013 6 SHADE CHIPS 9701639 25853678 PM9000015404 1 PM1000010934 1 1
2013 6 SHADE CHIPS 9701639 25853678 PM9000015404 1 PM1000010935 1 1
2013 6 SHADE CHIPS 9701639 25853678 PM9000015404 1 PM1000010936 1 1
2013 6 SHADE CHIPS 9701639 25853678 PM9000015404 1 PM1000010937 1 1
2013 6 SHADE CHIPS 9701639 25853678 PM9000015404 1 PM1000010938 1 1
2013 6 SHADE CHIPS 9701639 25853678 PM9000015404 1 PM1000010939 1 1
TOTALS 9 58 136
这是一张格式更好的照片:
最后,我想看到以下内容:
Year Month Class FG Units RM Units
2012 6 SHADE CHIPS 3449 50351
2012 7 SHADE CHIPS 288 3714
2012 8 SHADE CHIPS 282 4498
2012 9 SHADE CHIPS 105 1528
2012 12 SHADE CHIPS 539 4002
2013 1 SHADE CHIPS 1972 15284
2013 2 SHADE CHIPS 121 781
2013 3 SHADE CHIPS 60 808
2013 4 SHADE CHIPS 74 1335
2013 5 SHADE CHIPS 5 40
2013 6 FILLER SHADE 1 18
2013 6 SHADE CHIPS 4788 36790
2013 7 FILLER SHADE 1 18
2013 7 SHADE CHIPS 207 1600
我尝试按年月,班级,shipID,内部发货线,料品执行初始组,并取最大值FG_Qty和RM_Qty的总和。然后取出该结果并再次对其进行分组,这次只按年份分组,分类,然后将FG_Qty和RM_Qty相加。
注意:在一次传递中直接执行直接组不起作用,因为FG_QTY的总和被夸大了,因为在原始数据中,由于连接到BOM详细信息表,FG_QTY被复制到多行中。所以我需要每个内部装备线Nbr只计算一次FG_Qty。
答案 0 :(得分:1)
在不了解您的数据的情况下,我可能会使用一些CTE来做到这一点。
WITH RM AS (
SELECT YEAR, MONTH, CLASS, SUM(RM_QTY) AS total_rm_qty
FROM Shipment_Data SD
JOIN BOM_Header BH ON
sd.id = bh.id
JOIN BOM_Detail BD ON
bh.id = bd.id
GROUP BY YEAR, MONTH, CLASS
)
,FG AS (
SELECT YEAR, MONTH, CLASS, SUM(FG_QTY) AS total_fg_qty
FROM Shipment_Data SD
GROUP BY YEAR, MONTH, CLASS
)
SELECT FG.YEAR,FG.MONTH,FG.CLASS, fg.total_rm_qty, rm.total_rm_qty
FROM FG
JOIN RM ON
FG.YEAR=RM.YEAR
AND FG.MONTH=RM.MONTH
AND FG.CLASS=RM.CLASS
ORDER BY
FG.YEAR,
FG.MONTH,
FG.CLASS
基本上,您正在进行第1次通过,按年,月和类汇总原材料数量,然后对成品进行相同的操作,但不加入BOM详细信息表。一旦对每个进行了适当的汇总和分组,您就可以在适当的聚合级别加入它们。
答案 1 :(得分:0)
您似乎只想按年,月和类进行分组 - 例如:
SELECT YEAR, MONTH, CLASS, SUM(RM_QTY), SUM(FG_QTY) FROM SHIPMENT_DATA
GROUP BY YEAR, MONTH, CLASS
如果你没有TOTAL_RM_QTY,你可以做SUM(FG_QTY * RM_QTY_PER_FG_UNIT)
答案 2 :(得分:0)
这样的事情可以解决问题
SELECT ShipYear, ShipMonth, CLASS, SUM(FG_ITEM), SUM(FG_QTY)
FROM P
GROUP BY ShipYear, ShipMonth, CLASS
希望这是有帮助的
感谢
答案 3 :(得分:0)
您可以通过为每个内部装运行号(在一年,一个月和一个班级内,我认为)的行分配一个序号来执行此操作。然后,只需在值为1时总结值:
SELECT ShipYear, ShipMonth, CLASS, SUM(FG_ITEM), SUM(case when seqnum = 1 then FG_QTY end)
FROM (select p.*,
row_number() over (partition by ShipYear, ShipMonth, Class,
Internal_Shipment_Line_Num
order by (select NULL)) as seqnum
from P
GROUP BY ShipYear, ShipMonth, CLASS;
我不是100%确定分区子句是正确的 - 例如,您可能还需要Shipment
。但这应该是解决问题的良好开端。