select br.ownerId, br.ownerType, br.requestId, br.sourceRequestId, br.contractId badContractId, r.requestId goodRequestId, r.contractId goodContractId, r.assignedContractNbr from tbl_TPM_Requests r with (nolock) inner join tbl_TPM_Contracts ct with (nolock) on ct.contractId = r.contractId and ct.ownerId = r.ownerId and ct.ownerType = r.ownerType inner join tbl_TPM_Requests br with (nolock) on br.assignedContractNbr = r.assignedContractNbr and br.ownerId = r.ownerId and br.ownerType = r.ownerType and br.contractId = r.requestId and br.contractId <> ct.contractId update tbl_TPM_Requests set contractId = where ownerId = and ownerType = and requestId = and contractId =

Bad Contracts: #qmd_badContractIds.recordCount#

select ct.ownerId, ct.ownerType, ct.contractId, ct.contractName, ct.assignedContractNbr, ct.lastApprovedChainSequence, max( r.chainSequence ) chainSequence, count( r.requestId ) requests from tbl_TPM_Contracts ct with (nolock) inner join tbl_TPM_Requests r with (nolock) on r.contractId = ct.contractId and r.ownerId = ct.ownerId and r.ownerType = ct.ownerType and r.status = 'APPROVED' -- where ct.contractId = 7858 group by ct.ownerId, ct.ownerType, ct.contractId, ct.contractName, ct.assignedContractNbr, ct.lastApprovedChainSequence having coalesce( ct.lastApprovedChainSequence, 0 ) <> max( r.chainSequence )

#qmd_toFix.recordCount#

select * from qmd_startChain where sourceRequestId is null select * from qmd_startChain order by startDate update tbl_TPM_Requests set sourceRequestId = where ownerId = and ownerType = and requestId = select ct.ownerId, ct.ownerType, ct.contractId, ct.contractName, ct.assignedContractNbr, ct.lastApprovedChainSequence, max( r.chainSequence ) chainSequence, count( r.requestId ) requests from tbl_TPM_Contracts ct with (nolock) inner join tbl_TPM_Requests r with (nolock) on r.contractId = ct.contractId and r.ownerId = ct.ownerId and r.ownerType = ct.ownerType and r.status = 'APPROVED' where ct.contractId = group by ct.ownerId, ct.ownerType, ct.contractId, ct.contractName, ct.assignedContractNbr, ct.lastApprovedChainSequence