The expression that you want is a conditional count(distinct)
:
COUNT(DISTINCT CASE WHEN CAST(EVENT_TIMESTAMP AS DATE) - CONTRACT_EFFECTIVE_DATE = 2
THEN TmpNIMSalesForceDB.EMAIL
END) AS "2 day"
By leaving out the else
clause, the value is NULL
when the condition is not met. So, the distinct values of EMAIL
are being counted for the condition.