Since you are on SQL Server 2012 you can use a couple of the new windowing functions. with C1 as ( select T.team_id, case when M.winning_team_id is null then 'T' when M.winning_team_id = T.team_id then 'W' else 'L' end as streak_type, M.match_id from FantasyTeams as T inner join FantasyMatches as M on T.team_id = M.home_fantasy_team_id or T.team_id = M.away_fantasy_team_id ), C2 as ( select C1.team_id, C1.streak_type, C1.match_id, lag(C1.streak_type, 1, C1.streak_type) over(partition by C1.team_id order by C1.match_id desc) as lag_streak_type from C1 ), C3 as ( select C2.team_id, C2.streak_type, sum(case when C2.lag_streak_type = C2.streak_type then 0 else 1 end) over(partition by C2.team_id order by C2.match_id desc rows unbounded preceding) as streak_sum from C2 ) select C3.team_id, C3.streak_type, count(*) as streak_count from C3 where C3.streak_sum = 0 group by C3.team_id, C3.streak_type order by C3.team_id; [SQL Fiddle](http://www.sqlfiddle.com/#!6/c01c5/22)