select count(*), fsl_dstId from tbl_CDR_Distributors with (nolock) where ownerId = 1 and fsl_tablecode = 'MFR' and nullif( fsl_dstId, 0 ) is not null group by fsl_dstId having count( fsl_dstId ) > 1 select cdr_recordId from tbl_CDR_Distributors with (nolock) where ownerId = 1and fsl_tablecode = 'MFR' and fsl_dstId = select count( tpartnerId ) from tbl_MFR_TPF_Detail_MFR1 with (nolock) where tpartnerId in ( ) group by tpartnerId having count( tpartnerId ) > 10000