The structure of the table I'm working with:
BoM table (which product is required to make the finished product): Here is the @BomList table
ββββββββββββββββ¦ββββββββββββββ¦βββββββββββββββ
β ParentPartId β ChildPartId β ChildPartQty β
β βββββββββββββββ¬ββββββββββββββ¬βββββββββββββββ£
β MCD1 β 2000416027 β 2 β
β MCD1 β 2000316029 β 1 β
β MCD1 β 2001020022 β 1 β
ββββββββββββββββ©ββββββββββββββ©βββββββββββββββ
Work orders table (what you need to create):
βββββββββββββββ¦ββββββββββββββββββ¦βββββββββββββββ
β WorkOrderId β WorkOrderItemId β ParentPartId β
β ββββββββββββββ¬ββββββββββββββββββ¬βββββββββββββββ£
β 1234 β 6735 β MCD1 β
βββββββββββββββ©ββββββββββββββββββ©βββββββββββββββ
How much has been done for the order. The following is the version of the t_WorkCenterRouting table for cutting.
βββββββββββββββββββ¦ββββββββββββββ¦ββββββ
β WorkOrderItemId β ChildPartId β Qty β
β ββββββββββββββββββ¬ββββββββββββββ¬ββββββ£
β 6735 β 2000316029 β 1 β
β 6735 β 2001020022 β 1 β
β 6736 β 2000416027 β 10 β
β 6736 β 2000316029 β 3 β
β 6736 β 2001020022 β 3 β
β 6737 β 2000416027 β 1 β
β 6737 β 2000316029 β 1 β
β 6737 β 2001020022 β 1 β
βββββββββββββββββββ©ββββββββββββββ©ββββββ
The result I'm looking for
Thus, from the above table it can be seen that WorkOrderItem 6735 cannot make a complete product, because it is missing 2000416027 x2. Therefore, I do not want them to be displayed.
We can also see that WorkOrderItem 6737 cannot make a complete product because it is missing 2000416027 x1. Therefore, I do not want them to be displayed.
We also see that WorkOrderItem 6736 can make a total of x3 products.
.
, :
βββββββββββββββββββ¦ββββββ
β WorkOrderItemId β Qty β
β ββββββββββββββββββ¬ββββββ£
β 6737 β 3 β
βββββββββββββββββββ©ββββββ
, :
SELECT twcr.WorkOrderItemId,
bl.ChildPartQty,
SUM( ISNULL( twcr.Qty, 0 ) ) / bl.ChildPartQty AS 'TotalQty'
FROM @BomList bl
LEFT JOIN niko.t_WorkCenterRouting twcr ON twcr.ChildCode = bl.ChildPartId AND twcr.WorkCenterId = 4
WHERE (twcr.WorkCenterRoutingId IS NULL OR twcr.ParentCode = @ProductCode)
GROUP BY twcr.WorkOrderItemId, twcr.ChildCode, bl.ChildPartQty
, , 6735 2000416027. , , qty.
- , . .
....................
SELECT wcr.WorkOrderItemId,
( SELECT TOP 1 ROUND( SUM( ISNULL( twcr.Qty, 0 ) / bl.ChildPartQty ), 0, 1 )
FROM
LEFT JOIN niko.t_WorkCenterRouting twcr ON twcr.ChildCode = bl.ChildPartId AND twcr.WorkCenterId = 4 AND twcr.ParentCode = @WorkOrderCode AND wcr.WorkOrderItemId = twcr.WorkOrderItemId
GROUP BY twcr.WorkOrderItemId, bl.ChildPartId, bl.ChildPartQty
ORDER BY SUM( ISNULL( twcr.Qty, 0 ) / bl.ChildPartQty ) ) AS 'Qty'
FROM niko.t_WorkCenterRouting wcr
WHERE wcr.WorkOrderItemId IN ( SELECT twcr.WorkOrderItemId
FROM
LEFT JOIN niko.t_WorkCenterRouting twcr ON twcr.ChildCode = bl.ChildPartId AND twcr.WorkCenterId = 4 AND twcr.ParentCode = @WorkOrderCode
WHERE (twcr.WorkCenterRoutingId IS NULL OR twcr.ParentCode = @WorkOrderCode )
GROUP BY twcr.WorkOrderItemId, bl.ChildPartId, bl.ChildPartQty ) AND
( SELECT TOP 1 SUM( ISNULL( twcr.Qty, 0 ) / bl.ChildPartQty )
FROM
LEFT JOIN niko.t_WorkCenterRouting twcr ON twcr.ChildCode = bl.ChildPartId AND twcr.WorkCenterId = 4 AND twcr.ParentCode = @WorkOrderCode
GROUP BY twcr.WorkOrderItemId, bl.ChildPartId, bl.ChildPartQty
ORDER BY SUM( ISNULL( twcr.Qty, 0 ) / bl.ChildPartQty ) ) >= 1
GROUP BY wcr.WorkOrderItemId
. , -. , .
- WorkOrder
- WorkOrderItem
- t_WorkCenterRouting
- t_WorkCenterProductRoute
- ProductFF
- BoM