SELECT COUNT(*) FROM ( SELECT a.*, b.CMPY_N, b.CUST_ACNT_N FROM [BeaconPhase2_DEV].[BCN].[AGENTS_LOG_MSG] a LEFT JOIN [BeaconPhase2_DEV].[BCN].[CUST_STATIC] b ON a.CUST_K = b.CUST_K ) AS subquery;