You cannot use aliases in where , group by or having . You can get around this by wrapping it in a subquery:
SELECT * FROM ( SELECT t1.[user1], t1.[user2], (CAST(t1.[total_event_duration] AS DECIMAL)) / (CAST (t2.[total_events_duration] AS DECIMAL)) AS buddy_strength FROM [CDRs].[dbo].[aggregate_monthly_events] AS t1 INNER JOIN [CDRs].[dbo].[user_monthly_stats] AS t2 ON t1.[user1] = t2.[user1] ) a WHERE a.buddy_strength > 0.02
Otherwise, you will have to re-enter all this, which is not good.
source share