SELECT min(cs.purchorderNum) as PurchorderNum, cs.productID, (cs.purchOrderQty / p.weight) AS QtyOrder, (cs.qtyFilled / p.weight) AS QtyFilled, isActive, p.weight FROM CustomerServicePO cs INNER JOIN products p on cs.productID = p.productID INNER JOIN ContainerFillHistory cf on cs.customerID = cf.customerID WHERE SUBSTRING(@containerID, 1, 6) = cs.productID AND isActive = 1 GROUP BY cs.purchorderNum, cs.customerID, cs.productID, cs.qtyFilled, cs.purchOrderQty, isActive, p.weight INSERT ContainerFillHistory(containerID, lotID, productID, customerID, fillDateTime, purchorderNum, qtyFilled) VALUES (@containerID, @lotNum, SUBSTRING(@containerID, 1, 6), @customerID, @fillDateTime, PurchorderNum, QtyFilled UPDATE cs SET isActive = ( CASE WHEN (QtyOrder < QtyFilled) THEN 0 END ), QtyFilled = QtyFilled +1, cs.qtyFilled = QtyFilled * p.weight WHERE min(cs.purchorderNum) = PurchorderNum
I need to insert a mix of values and 2 column names that have been renamed because of a min function and a division calculation. How do I insert with VALUES and the renamed columns from the SELECT query?
The complete sequence of this code is here and the UPDATE statement is OK.