select ownerid, fsl_tablecode ownerType, 'DATA' anomalyType, 'reDistributionDetector' detectorName, cdr_dstname, cdr_dstname + ' (' + convert( varchar, cdr_recordId ) + ') is NOT mapped to 1FS' as message, 1 anomalyCount from tbl_CDR_Distributors where cdr_dstcompanytype = 'S' and cdr_specialAcctType = 'RDA' and nullif( fsl_dstId, 0 ) is null and ownerId > 0 union select ownerId, fsl_tablecode ownerType, 'DATA' anomalyType, 'reDistributionDetector' detectorName, cdr_dstname, cdr_dstname + ' (' + convert( varchar, cdr_recordId ) + ') is mapped to 1FS but NO corresponding branch distributor' as message, 1 anomalyCount from tbl_CDR_Distributors cdr where cdr.cdr_dstcompanytype = 'S' and cdr.cdr_specialAcctType = 'RDA' and nullif( fsl_dstId, 0 ) is not null and ( select count( cdrA.cdr_recordId ) from tbl_CDR_Distributors cdrA where cdrA.ownerId = cdr.ownerId and cdrA.fsl_tablecode = cdr.fsl_tablecode and cdrA.fsl_dstId = cdr.fsl_dstId ) = 1 and ownerId > 0 union select ownerId, fsl_tablecode ownerType, 'DATA' anomalyType, 'reDistributionDetector' detectorName, cdr_dstname, cdr_dstname + ' (' + convert( varchar, cdr_recordId ) + ') is not linked Re-Dist Account' as message, 1 anomalyCount from tbl_CDR_Distributors cdr where cdr.cdr_dstcompanytype = 'S' and cdr.cdr_specialAcctType = 'RDA' and nullif( cdr_reDistAcctFor, 0 ) is null and ownerId > 0 order by ownerId, ownerType, cdr_dstname