Update MDEPSet MDEP.POObligations =(select SUM(PerPO.SumOfPurchaseOrder) from(Select PO_MX.PONumber, PO_MX.MDEP, ((PO_MX.PaymentPosted * -1 ) + POD.SumOfLineItems) * (cast(PO_MX.PrimarySplit as decimal(5,2))/100) As SumOfPurchaseOrderfrom PO_MXinner join(select PONumber, SUM(PO_DX.QuantityOrdered * PO_DX.Price) As SumOfLineItemsfrom PO_DX join MDEP ON PO_MX.MDEP = MDEP.MDEP --add MDEP as a join herewhere (QuantityOrdered * Price) is not NULLgroup by PO_DX.PONumber)as PODon PO_MX.PONumber = POD.PONumber--where PO_MX.MDEP = MDEP.MDEP --move this to the ON clause above)as PerPO)where MDEP.MDEP In(select MDEP from PO_MX)

UPDATE m Set POObligations = x.POObligationsFROM MDEP mCROSS APPLY ( SELECT POObligations = (SUM(PO_DX.QuantityOrdered * PO_DX.Price) - SUM(Distinct PO_MX.PaymentPosted)) * SUM(Distinct(cast(PO_MX.PrimarySplit as decimal(5,2))/100)) FROM PO_MX INNER JOIN PO_DX ON PO_DX.PONumber = PO_MX.PONumber WHERE PO_MX.MDEP = m.MDEP) p

UPDATE m SET POObligations = p.POTotalFROM MDEP mCROSS APPLY ( SELECT POTotal = SUM((dx.LineItemTotal - mx.PaidToDate) * CAST(mx.PrimarySplit AS DECIMAL(5,2))/100) FROM PO_MX mx INNER JOIN ( SELECT PONumber, LineItemTotal = ISNULL(SUM(QuantityOrdered * Price),0) FROM PO_DX GROUP BY PONumber ) dx ON mx.PONumber = dx.PONumber WHERE mx.MDEP = m.MDEP) p