0

我正在查看过去 12 个月的装运数据,并想要装运的总成品单位及其原材料对应部件。

我已将装运详细信息表与物料清单标题(具有相应的成品项目)连接起来,然后将 BOM HEader 连接到 BOM 详细信息以获取每个成品单位的所有原材料组件和数量。

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。

4

4 回答 4

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

基本上,您正在做第一次通过年、月和类汇总原材料数量,然后对成品做同样的事情,但不加入 BOM 详细表。一旦每个都被适当地汇总和分组,您就可以在适当的聚合级别加入它们。

于 2013-07-25T22:40:53.917 回答
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)

于 2013-07-25T21:58:03.677 回答
0

像这样的东西应该可以解决问题

SELECT ShipYear, ShipMonth, CLASS, SUM(FG_ITEM), SUM(FG_QTY)
FROM P
GROUP BY  ShipYear, ShipMonth, CLASS

希望这有帮助

谢谢

于 2013-07-25T21:58:26.563 回答
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% 确定 partition 子句是正确的——例如,你可能也想要Shipment它。但这应该是解决您的问题的良好开端。

于 2013-07-25T22:24:47.800 回答