T-SQL Row Aggregation

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 #BomList bl
                                        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 #BomList bl
                                    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 #BomList bl
                        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
+4
1

, SELECT :

SELECT two.WorkOrderItemId, MIN(COALESCE(twcr.Qty/b.ChildPartQty, 0)) as N
FROM t_WorkOrder two
INNER JOIN BOM b on b.ParentpartId = two.ParentPartId
LEFT JOIN t_WorkCenterRouting twcr ON b.ChildPartId = twcr.ChildPartId AND two.WorkOrderItemId = twcr.workOrderItemId
GROUP BY two.WorkOrderItemId
HAVING MIN(COALESCE(twcr.Qty/b.ChildPartQty, 0)) > 0
ORDER BY two.WorkOrderItemId
0

Source: https://habr.com/ru/post/1547751/


All Articles