select c.ownerId, c.ownerType, c.claimId, pod.proofOfDeliveryId, c.claimStatus, cdr.cdr_RecordId, cdr.cdr_dstName, cdr.cdr_dstCode, pod.distributorCode, pod.distributorName, pod.cdrRecordId from tbl_TPM_Claims c inner join tbl_TPM_ProofOfDelivery pod on pod.claimId = c.claimId and pod.ownerId = c.ownerId inner join tbl_CDR_Distributors cdr on cdr.cdr_recordId = c.partnerId and cdr.ownerId = c.ownerId and cdr.cdr_dstCode = pod.distributorCode where c.edi = 'Y' and c.ownerId = and c.ownerType = 'MFR' and c.partnerType = 'CDR' and c.partnerId in ( select cdr.cdr_recordId from tbl_CDR_Distributors cdr inner join tbl_DST_Distributors dst on dst.dstId = cdr.fsl_dstId and dst.dstParentCo = 4036 ) -- and pod.flagged = 'Y' -- and pod.flagComment like 'Missing%' and pod.cdrRecordId is null and c.reconciledAmount is null and (( c.claimStatus = 'APPROVED' and c.deductionRef = '11911' ) or c.claimStatus = 'OPEN' or c.claimStatus = 'HOLD' ) order by c.claimId update tbl_TPM_ProofOfDelivery set cdrRecordId = where proofOfDeliveryId = and claimId = and ownerId = and ownerType = and cdrRecordId is null update tbl_TPM_Claims set claimStatus = 'OPEN' where claimStatus = 'APPROVED' and claimId = and ownerId = and ownerType =