select distinct cdr.ownerId, cdr.fsl_tablecode, cdr.fsl_dstId from tbl_CDR_Distributors cdr with (nolock) inner join tbl_DST_Distributors dst with (nolock) on cdr.fsl_dstId = dst.dstId where cdr.ownerId > 0 and nullif( cdr.fsl_dstId, 0 ) is not null select fsl_mapPriority from tbl_CDR_Distributors with (nolock) where ownerId = and fsl_tablecode = and fsl_dstId = order by fsl_mapPriority