Min(date) in sum case statement
DECLARE @myDate AS datetime
SELECT @myDate = min(cast(f.datedue as DATE)) FROM tablef f
SELECT 
    sum(case when cast(l.settleddate AS date) &lt; @myDate
             Then 1 
             else 0      
             end) as BeforeFirstDue
FROM tablel l