changed last LEFT JOIN to a LEFT MERGE JOIN and change CREATE dbo.EVENTS to DECLARE @EVENTS TABLE()